xref: /openbmc/linux/drivers/hwmon/occ/common.c (revision 69fad28c)
1 // SPDX-License-Identifier: GPL-2.0
2 
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>
12 
13 #include "common.h"
14 
15 #define EXTN_FLAG_SENSOR_ID		BIT(7)
16 
17 #define OCC_ERROR_COUNT_THRESHOLD	2	/* required by OCC spec */
18 
19 #define OCC_STATE_SAFE			4
20 #define OCC_SAFE_TIMEOUT		msecs_to_jiffies(60000) /* 1 min */
21 
22 #define OCC_UPDATE_FREQUENCY		msecs_to_jiffies(1000)
23 
24 #define OCC_TEMP_SENSOR_FAULT		0xFF
25 
26 #define OCC_FRU_TYPE_VRM		3
27 
28 /* OCC sensor type and version definitions */
29 
30 struct temp_sensor_1 {
31 	u16 sensor_id;
32 	u16 value;
33 } __packed;
34 
35 struct temp_sensor_2 {
36 	u32 sensor_id;
37 	u8 fru_type;
38 	u8 value;
39 } __packed;
40 
41 struct freq_sensor_1 {
42 	u16 sensor_id;
43 	u16 value;
44 } __packed;
45 
46 struct freq_sensor_2 {
47 	u32 sensor_id;
48 	u16 value;
49 } __packed;
50 
51 struct power_sensor_1 {
52 	u16 sensor_id;
53 	u32 update_tag;
54 	u32 accumulator;
55 	u16 value;
56 } __packed;
57 
58 struct power_sensor_2 {
59 	u32 sensor_id;
60 	u8 function_id;
61 	u8 apss_channel;
62 	u16 reserved;
63 	u32 update_tag;
64 	u64 accumulator;
65 	u16 value;
66 } __packed;
67 
68 struct power_sensor_data {
69 	u16 value;
70 	u32 update_tag;
71 	u64 accumulator;
72 } __packed;
73 
74 struct power_sensor_data_and_time {
75 	u16 update_time;
76 	u16 value;
77 	u32 update_tag;
78 	u64 accumulator;
79 } __packed;
80 
81 struct power_sensor_a0 {
82 	u32 sensor_id;
83 	struct power_sensor_data_and_time system;
84 	u32 reserved;
85 	struct power_sensor_data_and_time proc;
86 	struct power_sensor_data vdd;
87 	struct power_sensor_data vdn;
88 } __packed;
89 
90 struct caps_sensor_2 {
91 	u16 cap;
92 	u16 system_power;
93 	u16 n_cap;
94 	u16 max;
95 	u16 min;
96 	u16 user;
97 	u8 user_source;
98 } __packed;
99 
100 struct caps_sensor_3 {
101 	u16 cap;
102 	u16 system_power;
103 	u16 n_cap;
104 	u16 max;
105 	u16 hard_min;
106 	u16 soft_min;
107 	u16 user;
108 	u8 user_source;
109 } __packed;
110 
111 struct extended_sensor {
112 	union {
113 		u8 name[4];
114 		u32 sensor_id;
115 	};
116 	u8 flags;
117 	u8 reserved;
118 	u8 data[6];
119 } __packed;
120 
121 static int occ_poll(struct occ *occ)
122 {
123 	int rc;
124 	u16 checksum = occ->poll_cmd_data + 1;
125 	u8 cmd[8];
126 	struct occ_poll_response_header *header;
127 
128 	/* big endian */
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 */
136 	cmd[7] = 0;
137 
138 	/* mutex should already be locked if necessary */
139 	rc = occ->send_cmd(occ, cmd);
140 	if (rc) {
141 		if (occ->error_count++ > OCC_ERROR_COUNT_THRESHOLD)
142 			occ->error = rc;
143 
144 		goto done;
145 	}
146 
147 	/* clear error since communication was successful */
148 	occ->error_count = 0;
149 	occ->error = 0;
150 
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;
158 		} else {
159 			occ->last_safe = jiffies;
160 		}
161 	} else {
162 		occ->last_safe = 0;
163 	}
164 
165 done:
166 	occ_sysfs_poll_done(occ);
167 	return rc;
168 }
169 
170 static int occ_set_user_power_cap(struct occ *occ, u16 user_power_cap)
171 {
172 	int rc;
173 	u8 cmd[8];
174 	u16 checksum = 0x24;
175 	__be16 user_power_cap_be = cpu_to_be16(user_power_cap);
176 
177 	cmd[0] = 0;
178 	cmd[1] = 0x22;
179 	cmd[2] = 0;
180 	cmd[3] = 2;
181 
182 	memcpy(&cmd[4], &user_power_cap_be, 2);
183 
184 	checksum += cmd[4] + cmd[5];
185 	cmd[6] = checksum >> 8;
186 	cmd[7] = checksum & 0xFF;
187 
188 	rc = mutex_lock_interruptible(&occ->lock);
189 	if (rc)
190 		return rc;
191 
192 	rc = occ->send_cmd(occ, cmd);
193 
194 	mutex_unlock(&occ->lock);
195 
196 	return rc;
197 }
198 
199 int occ_update_response(struct occ *occ)
200 {
201 	int rc = mutex_lock_interruptible(&occ->lock);
202 
203 	if (rc)
204 		return rc;
205 
206 	/* limit the maximum rate of polling the OCC */
207 	if (time_after(jiffies, occ->last_update + OCC_UPDATE_FREQUENCY)) {
208 		rc = occ_poll(occ);
209 		occ->last_update = jiffies;
210 	}
211 
212 	mutex_unlock(&occ->lock);
213 	return rc;
214 }
215 
216 static ssize_t occ_show_temp_1(struct device *dev,
217 			       struct device_attribute *attr, char *buf)
218 {
219 	int rc;
220 	u32 val = 0;
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);
225 
226 	rc = occ_update_response(occ);
227 	if (rc)
228 		return rc;
229 
230 	temp = ((struct temp_sensor_1 *)sensors->temp.data) + sattr->index;
231 
232 	switch (sattr->nr) {
233 	case 0:
234 		val = get_unaligned_be16(&temp->sensor_id);
235 		break;
236 	case 1:
237 		val = get_unaligned_be16(&temp->value) * 1000;
238 		break;
239 	default:
240 		return -EINVAL;
241 	}
242 
243 	return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
244 }
245 
246 static ssize_t occ_show_temp_2(struct device *dev,
247 			       struct device_attribute *attr, char *buf)
248 {
249 	int rc;
250 	u32 val = 0;
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);
255 
256 	rc = occ_update_response(occ);
257 	if (rc)
258 		return rc;
259 
260 	temp = ((struct temp_sensor_2 *)sensors->temp.data) + sattr->index;
261 
262 	switch (sattr->nr) {
263 	case 0:
264 		val = get_unaligned_be32(&temp->sensor_id);
265 		break;
266 	case 1:
267 		val = temp->value;
268 		if (val == OCC_TEMP_SENSOR_FAULT)
269 			return -EREMOTEIO;
270 
271 		/*
272 		 * VRM doesn't return temperature, only alarm bit. This
273 		 * attribute maps to tempX_alarm instead of tempX_input for
274 		 * VRM
275 		 */
276 		if (temp->fru_type != OCC_FRU_TYPE_VRM) {
277 			/* sensor not ready */
278 			if (val == 0)
279 				return -EAGAIN;
280 
281 			val *= 1000;
282 		}
283 		break;
284 	case 2:
285 		val = temp->fru_type;
286 		break;
287 	case 3:
288 		val = temp->value == OCC_TEMP_SENSOR_FAULT;
289 		break;
290 	default:
291 		return -EINVAL;
292 	}
293 
294 	return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
295 }
296 
297 static ssize_t occ_show_freq_1(struct device *dev,
298 			       struct device_attribute *attr, char *buf)
299 {
300 	int rc;
301 	u16 val = 0;
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);
306 
307 	rc = occ_update_response(occ);
308 	if (rc)
309 		return rc;
310 
311 	freq = ((struct freq_sensor_1 *)sensors->freq.data) + sattr->index;
312 
313 	switch (sattr->nr) {
314 	case 0:
315 		val = get_unaligned_be16(&freq->sensor_id);
316 		break;
317 	case 1:
318 		val = get_unaligned_be16(&freq->value);
319 		break;
320 	default:
321 		return -EINVAL;
322 	}
323 
324 	return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
325 }
326 
327 static ssize_t occ_show_freq_2(struct device *dev,
328 			       struct device_attribute *attr, char *buf)
329 {
330 	int rc;
331 	u32 val = 0;
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);
336 
337 	rc = occ_update_response(occ);
338 	if (rc)
339 		return rc;
340 
341 	freq = ((struct freq_sensor_2 *)sensors->freq.data) + sattr->index;
342 
343 	switch (sattr->nr) {
344 	case 0:
345 		val = get_unaligned_be32(&freq->sensor_id);
346 		break;
347 	case 1:
348 		val = get_unaligned_be16(&freq->value);
349 		break;
350 	default:
351 		return -EINVAL;
352 	}
353 
354 	return snprintf(buf, PAGE_SIZE - 1, "%u\n", val);
355 }
356 
357 static ssize_t occ_show_power_1(struct device *dev,
358 				struct device_attribute *attr, char *buf)
359 {
360 	int rc;
361 	u64 val = 0;
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);
366 
367 	rc = occ_update_response(occ);
368 	if (rc)
369 		return rc;
370 
371 	power = ((struct power_sensor_1 *)sensors->power.data) + sattr->index;
372 
373 	switch (sattr->nr) {
374 	case 0:
375 		val = get_unaligned_be16(&power->sensor_id);
376 		break;
377 	case 1:
378 		val = get_unaligned_be32(&power->accumulator) /
379 			get_unaligned_be32(&power->update_tag);
380 		val *= 1000000ULL;
381 		break;
382 	case 2:
383 		val = get_unaligned_be32(&power->update_tag) *
384 			occ->powr_sample_time_us;
385 		break;
386 	case 3:
387 		val = get_unaligned_be16(&power->value) * 1000000ULL;
388 		break;
389 	default:
390 		return -EINVAL;
391 	}
392 
393 	return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
394 }
395 
396 static u64 occ_get_powr_avg(u64 *accum, u32 *samples)
397 {
398 	return div64_u64(get_unaligned_be64(accum) * 1000000ULL,
399 			 get_unaligned_be32(samples));
400 }
401 
402 static ssize_t occ_show_power_2(struct device *dev,
403 				struct device_attribute *attr, char *buf)
404 {
405 	int rc;
406 	u64 val = 0;
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);
411 
412 	rc = occ_update_response(occ);
413 	if (rc)
414 		return rc;
415 
416 	power = ((struct power_sensor_2 *)sensors->power.data) + sattr->index;
417 
418 	switch (sattr->nr) {
419 	case 0:
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);
423 	case 1:
424 		val = occ_get_powr_avg(&power->accumulator,
425 				       &power->update_tag);
426 		break;
427 	case 2:
428 		val = get_unaligned_be32(&power->update_tag) *
429 			occ->powr_sample_time_us;
430 		break;
431 	case 3:
432 		val = get_unaligned_be16(&power->value) * 1000000ULL;
433 		break;
434 	default:
435 		return -EINVAL;
436 	}
437 
438 	return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
439 }
440 
441 static ssize_t occ_show_power_a0(struct device *dev,
442 				 struct device_attribute *attr, char *buf)
443 {
444 	int rc;
445 	u64 val = 0;
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);
450 
451 	rc = occ_update_response(occ);
452 	if (rc)
453 		return rc;
454 
455 	power = ((struct power_sensor_a0 *)sensors->power.data) + sattr->index;
456 
457 	switch (sattr->nr) {
458 	case 0:
459 		return snprintf(buf, PAGE_SIZE - 1, "%u_system\n",
460 				get_unaligned_be32(&power->sensor_id));
461 	case 1:
462 		val = occ_get_powr_avg(&power->system.accumulator,
463 				       &power->system.update_tag);
464 		break;
465 	case 2:
466 		val = get_unaligned_be32(&power->system.update_tag) *
467 			occ->powr_sample_time_us;
468 		break;
469 	case 3:
470 		val = get_unaligned_be16(&power->system.value) * 1000000ULL;
471 		break;
472 	case 4:
473 		return snprintf(buf, PAGE_SIZE - 1, "%u_proc\n",
474 				get_unaligned_be32(&power->sensor_id));
475 	case 5:
476 		val = occ_get_powr_avg(&power->proc.accumulator,
477 				       &power->proc.update_tag);
478 		break;
479 	case 6:
480 		val = get_unaligned_be32(&power->proc.update_tag) *
481 			occ->powr_sample_time_us;
482 		break;
483 	case 7:
484 		val = get_unaligned_be16(&power->proc.value) * 1000000ULL;
485 		break;
486 	case 8:
487 		return snprintf(buf, PAGE_SIZE - 1, "%u_vdd\n",
488 				get_unaligned_be32(&power->sensor_id));
489 	case 9:
490 		val = occ_get_powr_avg(&power->vdd.accumulator,
491 				       &power->vdd.update_tag);
492 		break;
493 	case 10:
494 		val = get_unaligned_be32(&power->vdd.update_tag) *
495 			occ->powr_sample_time_us;
496 		break;
497 	case 11:
498 		val = get_unaligned_be16(&power->vdd.value) * 1000000ULL;
499 		break;
500 	case 12:
501 		return snprintf(buf, PAGE_SIZE - 1, "%u_vdn\n",
502 				get_unaligned_be32(&power->sensor_id));
503 	case 13:
504 		val = occ_get_powr_avg(&power->vdn.accumulator,
505 				       &power->vdn.update_tag);
506 		break;
507 	case 14:
508 		val = get_unaligned_be32(&power->vdn.update_tag) *
509 			occ->powr_sample_time_us;
510 		break;
511 	case 15:
512 		val = get_unaligned_be16(&power->vdn.value) * 1000000ULL;
513 		break;
514 	default:
515 		return -EINVAL;
516 	}
517 
518 	return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
519 }
520 
521 static ssize_t occ_show_caps_1_2(struct device *dev,
522 				 struct device_attribute *attr, char *buf)
523 {
524 	int rc;
525 	u64 val = 0;
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);
530 
531 	rc = occ_update_response(occ);
532 	if (rc)
533 		return rc;
534 
535 	caps = ((struct caps_sensor_2 *)sensors->caps.data) + sattr->index;
536 
537 	switch (sattr->nr) {
538 	case 0:
539 		return snprintf(buf, PAGE_SIZE - 1, "system\n");
540 	case 1:
541 		val = get_unaligned_be16(&caps->cap) * 1000000ULL;
542 		break;
543 	case 2:
544 		val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
545 		break;
546 	case 3:
547 		val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
548 		break;
549 	case 4:
550 		val = get_unaligned_be16(&caps->max) * 1000000ULL;
551 		break;
552 	case 5:
553 		val = get_unaligned_be16(&caps->min) * 1000000ULL;
554 		break;
555 	case 6:
556 		val = get_unaligned_be16(&caps->user) * 1000000ULL;
557 		break;
558 	case 7:
559 		if (occ->sensors.caps.version == 1)
560 			return -EINVAL;
561 
562 		val = caps->user_source;
563 		break;
564 	default:
565 		return -EINVAL;
566 	}
567 
568 	return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
569 }
570 
571 static ssize_t occ_show_caps_3(struct device *dev,
572 			       struct device_attribute *attr, char *buf)
573 {
574 	int rc;
575 	u64 val = 0;
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);
580 
581 	rc = occ_update_response(occ);
582 	if (rc)
583 		return rc;
584 
585 	caps = ((struct caps_sensor_3 *)sensors->caps.data) + sattr->index;
586 
587 	switch (sattr->nr) {
588 	case 0:
589 		return snprintf(buf, PAGE_SIZE - 1, "system\n");
590 	case 1:
591 		val = get_unaligned_be16(&caps->cap) * 1000000ULL;
592 		break;
593 	case 2:
594 		val = get_unaligned_be16(&caps->system_power) * 1000000ULL;
595 		break;
596 	case 3:
597 		val = get_unaligned_be16(&caps->n_cap) * 1000000ULL;
598 		break;
599 	case 4:
600 		val = get_unaligned_be16(&caps->max) * 1000000ULL;
601 		break;
602 	case 5:
603 		val = get_unaligned_be16(&caps->hard_min) * 1000000ULL;
604 		break;
605 	case 6:
606 		val = get_unaligned_be16(&caps->user) * 1000000ULL;
607 		break;
608 	case 7:
609 		val = caps->user_source;
610 		break;
611 	default:
612 		return -EINVAL;
613 	}
614 
615 	return snprintf(buf, PAGE_SIZE - 1, "%llu\n", val);
616 }
617 
618 static ssize_t occ_store_caps_user(struct device *dev,
619 				   struct device_attribute *attr,
620 				   const char *buf, size_t count)
621 {
622 	int rc;
623 	u16 user_power_cap;
624 	unsigned long long value;
625 	struct occ *occ = dev_get_drvdata(dev);
626 
627 	rc = kstrtoull(buf, 0, &value);
628 	if (rc)
629 		return rc;
630 
631 	user_power_cap = div64_u64(value, 1000000ULL); /* microwatt to watt */
632 
633 	rc = occ_set_user_power_cap(occ, user_power_cap);
634 	if (rc)
635 		return rc;
636 
637 	return count;
638 }
639 
640 static ssize_t occ_show_extended(struct device *dev,
641 				 struct device_attribute *attr, char *buf)
642 {
643 	int rc;
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);
648 
649 	rc = occ_update_response(occ);
650 	if (rc)
651 		return rc;
652 
653 	extn = ((struct extended_sensor *)sensors->extended.data) +
654 		sattr->index;
655 
656 	switch (sattr->nr) {
657 	case 0:
658 		if (extn->flags & EXTN_FLAG_SENSOR_ID)
659 			rc = snprintf(buf, PAGE_SIZE - 1, "%u",
660 				      get_unaligned_be32(&extn->sensor_id));
661 		else
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]);
665 		break;
666 	case 1:
667 		rc = snprintf(buf, PAGE_SIZE - 1, "%02x\n", extn->flags);
668 		break;
669 	case 2:
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]);
673 		break;
674 	default:
675 		return -EINVAL;
676 	}
677 
678 	return rc;
679 }
680 
681 /*
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.
685  */
686 #define ATTR_OCC(_name, _mode, _show, _store) {				\
687 	.attr	= {							\
688 		.name = _name,						\
689 		.mode = VERIFY_OCTAL_PERMISSIONS(_mode),		\
690 	},								\
691 	.show	= _show,						\
692 	.store	= _store,						\
693 }
694 
695 #define SENSOR_ATTR_OCC(_name, _mode, _show, _store, _nr, _index) {	\
696 	.dev_attr	= ATTR_OCC(_name, _mode, _show, _store),	\
697 	.index		= _index,					\
698 	.nr		= _nr,						\
699 }
700 
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))
704 
705 /*
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.
708  */
709 static int occ_setup_sensor_attrs(struct occ *occ)
710 {
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;
724 
725 	switch (sensors->temp.version) {
726 	case 1:
727 		num_attrs += (sensors->temp.num_sensors * 2);
728 		break;
729 	case 2:
730 		num_attrs += (sensors->temp.num_sensors * 4);
731 		show_temp = occ_show_temp_2;
732 		break;
733 	default:
734 		sensors->temp.num_sensors = 0;
735 	}
736 
737 	switch (sensors->freq.version) {
738 	case 2:
739 		show_freq = occ_show_freq_2;
740 		/* fall through */
741 	case 1:
742 		num_attrs += (sensors->freq.num_sensors * 2);
743 		break;
744 	default:
745 		sensors->freq.num_sensors = 0;
746 	}
747 
748 	switch (sensors->power.version) {
749 	case 2:
750 		show_power = occ_show_power_2;
751 		/* fall through */
752 	case 1:
753 		num_attrs += (sensors->power.num_sensors * 4);
754 		break;
755 	case 0xA0:
756 		num_attrs += (sensors->power.num_sensors * 16);
757 		show_power = occ_show_power_a0;
758 		break;
759 	default:
760 		sensors->power.num_sensors = 0;
761 	}
762 
763 	switch (sensors->caps.version) {
764 	case 1:
765 		num_attrs += (sensors->caps.num_sensors * 7);
766 		break;
767 	case 3:
768 		show_caps = occ_show_caps_3;
769 		/* fall through */
770 	case 2:
771 		num_attrs += (sensors->caps.num_sensors * 8);
772 		break;
773 	default:
774 		sensors->caps.num_sensors = 0;
775 	}
776 
777 	switch (sensors->extended.version) {
778 	case 1:
779 		num_attrs += (sensors->extended.num_sensors * 3);
780 		break;
781 	default:
782 		sensors->extended.num_sensors = 0;
783 	}
784 
785 	occ->attrs = devm_kzalloc(dev, sizeof(*occ->attrs) * num_attrs,
786 				  GFP_KERNEL);
787 	if (!occ->attrs)
788 		return -ENOMEM;
789 
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)
794 		return -ENOMEM;
795 
796 	attr = occ->attrs;
797 
798 	for (i = 0; i < sensors->temp.num_sensors; ++i) {
799 		s = i + 1;
800 		temp = ((struct temp_sensor_2 *)sensors->temp.data) + i;
801 
802 		snprintf(attr->name, sizeof(attr->name), "temp%d_label", s);
803 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
804 					     0, i);
805 		attr++;
806 
807 		if (sensors->temp.version > 1 &&
808 		    temp->fru_type == OCC_FRU_TYPE_VRM) {
809 			snprintf(attr->name, sizeof(attr->name),
810 				 "temp%d_alarm", s);
811 		} else {
812 			snprintf(attr->name, sizeof(attr->name),
813 				 "temp%d_input", s);
814 		}
815 
816 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_temp, NULL,
817 					     1, i);
818 		attr++;
819 
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);
825 			attr++;
826 
827 			snprintf(attr->name, sizeof(attr->name),
828 				 "temp%d_fault", s);
829 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
830 						     show_temp, NULL, 3, i);
831 			attr++;
832 		}
833 	}
834 
835 	for (i = 0; i < sensors->freq.num_sensors; ++i) {
836 		s = i + 1;
837 
838 		snprintf(attr->name, sizeof(attr->name), "freq%d_label", s);
839 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
840 					     0, i);
841 		attr++;
842 
843 		snprintf(attr->name, sizeof(attr->name), "freq%d_input", s);
844 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_freq, NULL,
845 					     1, i);
846 		attr++;
847 	}
848 
849 	if (sensors->power.version == 0xA0) {
850 		/*
851 		 * Special case for many-attribute power sensor. Split it into
852 		 * a sensor number per power type, emulating several sensors.
853 		 */
854 		for (i = 0; i < sensors->power.num_sensors; ++i) {
855 			unsigned int j;
856 			unsigned int nr = 0;
857 
858 			s = (i * 4) + 1;
859 
860 			for (j = 0; j < 4; ++j) {
861 				snprintf(attr->name, sizeof(attr->name),
862 					 "power%d_label", s);
863 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
864 							     show_power, NULL,
865 							     nr++, i);
866 				attr++;
867 
868 				snprintf(attr->name, sizeof(attr->name),
869 					 "power%d_average", s);
870 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
871 							     show_power, NULL,
872 							     nr++, i);
873 				attr++;
874 
875 				snprintf(attr->name, sizeof(attr->name),
876 					 "power%d_average_interval", s);
877 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
878 							     show_power, NULL,
879 							     nr++, i);
880 				attr++;
881 
882 				snprintf(attr->name, sizeof(attr->name),
883 					 "power%d_input", s);
884 				attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
885 							     show_power, NULL,
886 							     nr++, i);
887 				attr++;
888 
889 				s++;
890 			}
891 		}
892 	} else {
893 		for (i = 0; i < sensors->power.num_sensors; ++i) {
894 			s = i + 1;
895 
896 			snprintf(attr->name, sizeof(attr->name),
897 				 "power%d_label", s);
898 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
899 						     show_power, NULL, 0, i);
900 			attr++;
901 
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);
906 			attr++;
907 
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);
912 			attr++;
913 
914 			snprintf(attr->name, sizeof(attr->name),
915 				 "power%d_input", s);
916 			attr->sensor = OCC_INIT_ATTR(attr->name, 0444,
917 						     show_power, NULL, 3, i);
918 			attr++;
919 		}
920 	}
921 
922 	if (sensors->caps.num_sensors >= 1) {
923 		s = sensors->power.num_sensors + 1;
924 
925 		snprintf(attr->name, sizeof(attr->name), "power%d_label", s);
926 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
927 					     0, 0);
928 		attr++;
929 
930 		snprintf(attr->name, sizeof(attr->name), "power%d_cap", s);
931 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
932 					     1, 0);
933 		attr++;
934 
935 		snprintf(attr->name, sizeof(attr->name), "power%d_input", s);
936 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
937 					     2, 0);
938 		attr++;
939 
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,
943 					     3, 0);
944 		attr++;
945 
946 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_max", s);
947 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
948 					     4, 0);
949 		attr++;
950 
951 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_min", s);
952 		attr->sensor = OCC_INIT_ATTR(attr->name, 0444, show_caps, NULL,
953 					     5, 0);
954 		attr++;
955 
956 		snprintf(attr->name, sizeof(attr->name), "power%d_cap_user",
957 			 s);
958 		attr->sensor = OCC_INIT_ATTR(attr->name, 0644, show_caps,
959 					     occ_store_caps_user, 6, 0);
960 		attr++;
961 
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);
967 			attr++;
968 		}
969 	}
970 
971 	for (i = 0; i < sensors->extended.num_sensors; ++i) {
972 		s = i + 1;
973 
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);
977 		attr++;
978 
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);
982 		attr++;
983 
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);
987 		attr++;
988 	}
989 
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;
994 	}
995 
996 	return 0;
997 }
998 
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)
1001 {
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;
1010 
1011 	dev_info(occ->bus_dev, "OCC found, code level: %.16s\n",
1012 		 header->occ_code_level);
1013 
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);
1019 		size += offset;
1020 
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");
1024 			return;
1025 		}
1026 
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);
1030 
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;
1042 		else {
1043 			dev_warn(occ->bus_dev, "sensor not supported %.4s\n",
1044 				 block->header.eye_catcher);
1045 			continue;
1046 		}
1047 
1048 		sensor->num_sensors = block->header.num_sensors;
1049 		sensor->version = block->header.sensor_format;
1050 		sensor->data = &block->data;
1051 	}
1052 
1053 	dev_dbg(occ->bus_dev, "Max resp size: %u+%zd=%zd\n", size,
1054 		sizeof(*header), size + sizeof(*header));
1055 }
1056 
1057 int occ_setup(struct occ *occ, const char *name)
1058 {
1059 	int rc;
1060 
1061 	mutex_init(&occ->lock);
1062 	occ->groups[0] = &occ->group;
1063 
1064 	/* no need to lock */
1065 	rc = occ_poll(occ);
1066 	if (rc == -ESHUTDOWN) {
1067 		dev_info(occ->bus_dev, "host is not ready\n");
1068 		return rc;
1069 	} else if (rc < 0) {
1070 		dev_err(occ->bus_dev, "failed to get OCC poll response: %d\n",
1071 			rc);
1072 		return rc;
1073 	}
1074 
1075 	occ_parse_poll_response(occ);
1076 
1077 	rc = occ_setup_sensor_attrs(occ);
1078 	if (rc) {
1079 		dev_err(occ->bus_dev, "failed to setup sensor attrs: %d\n",
1080 			rc);
1081 		return rc;
1082 	}
1083 
1084 	occ->hwmon = devm_hwmon_device_register_with_groups(occ->bus_dev, name,
1085 							    occ, occ->groups);
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",
1089 			rc);
1090 		return rc;
1091 	}
1092 
1093 	rc = occ_setup_sysfs(occ);
1094 	if (rc)
1095 		dev_err(occ->bus_dev, "failed to setup sysfs: %d\n", rc);
1096 
1097 	return rc;
1098 }
1099