Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / hwmon / pmbus / pmbus_core.c
blob192442b3b7a287424977d1f035a7378c06df57c2
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Hardware monitoring driver for PMBus devices
5 * Copyright (c) 2010, 2011 Ericsson AB.
6 * Copyright (c) 2012 Guenter Roeck
7 */
9 #include <linux/debugfs.h>
10 #include <linux/kernel.h>
11 #include <linux/math64.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/hwmon.h>
18 #include <linux/hwmon-sysfs.h>
19 #include <linux/pmbus.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include "pmbus.h"
25 * Number of additional attribute pointers to allocate
26 * with each call to krealloc
28 #define PMBUS_ATTR_ALLOC_SIZE 32
29 #define PMBUS_NAME_SIZE 24
31 struct pmbus_sensor {
32 struct pmbus_sensor *next;
33 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */
34 struct device_attribute attribute;
35 u8 page; /* page number */
36 u8 phase; /* phase number, 0xff for all phases */
37 u16 reg; /* register */
38 enum pmbus_sensor_classes class; /* sensor class */
39 bool update; /* runtime sensor update needed */
40 bool convert; /* Whether or not to apply linear/vid/direct */
41 int data; /* Sensor data.
42 Negative if there was a read error */
44 #define to_pmbus_sensor(_attr) \
45 container_of(_attr, struct pmbus_sensor, attribute)
47 struct pmbus_boolean {
48 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */
49 struct sensor_device_attribute attribute;
50 struct pmbus_sensor *s1;
51 struct pmbus_sensor *s2;
53 #define to_pmbus_boolean(_attr) \
54 container_of(_attr, struct pmbus_boolean, attribute)
56 struct pmbus_label {
57 char name[PMBUS_NAME_SIZE]; /* sysfs label name */
58 struct device_attribute attribute;
59 char label[PMBUS_NAME_SIZE]; /* label */
61 #define to_pmbus_label(_attr) \
62 container_of(_attr, struct pmbus_label, attribute)
64 /* Macros for converting between sensor index and register/page/status mask */
66 #define PB_STATUS_MASK 0xffff
67 #define PB_REG_SHIFT 16
68 #define PB_REG_MASK 0x3ff
69 #define PB_PAGE_SHIFT 26
70 #define PB_PAGE_MASK 0x3f
72 #define pb_reg_to_index(page, reg, mask) (((page) << PB_PAGE_SHIFT) | \
73 ((reg) << PB_REG_SHIFT) | (mask))
75 #define pb_index_to_page(index) (((index) >> PB_PAGE_SHIFT) & PB_PAGE_MASK)
76 #define pb_index_to_reg(index) (((index) >> PB_REG_SHIFT) & PB_REG_MASK)
77 #define pb_index_to_mask(index) ((index) & PB_STATUS_MASK)
79 struct pmbus_data {
80 struct device *dev;
81 struct device *hwmon_dev;
83 u32 flags; /* from platform data */
85 int exponent[PMBUS_PAGES];
86 /* linear mode: exponent for output voltages */
88 const struct pmbus_driver_info *info;
90 int max_attributes;
91 int num_attributes;
92 struct attribute_group group;
93 const struct attribute_group **groups;
94 struct dentry *debugfs; /* debugfs device directory */
96 struct pmbus_sensor *sensors;
98 struct mutex update_lock;
100 bool has_status_word; /* device uses STATUS_WORD register */
101 int (*read_status)(struct i2c_client *client, int page);
103 s16 currpage; /* current page, -1 for unknown/unset */
104 s16 currphase; /* current phase, 0xff for all, -1 for unknown/unset */
107 struct pmbus_debugfs_entry {
108 struct i2c_client *client;
109 u8 page;
110 u8 reg;
113 static const int pmbus_fan_rpm_mask[] = {
114 PB_FAN_1_RPM,
115 PB_FAN_2_RPM,
116 PB_FAN_1_RPM,
117 PB_FAN_2_RPM,
120 static const int pmbus_fan_config_registers[] = {
121 PMBUS_FAN_CONFIG_12,
122 PMBUS_FAN_CONFIG_12,
123 PMBUS_FAN_CONFIG_34,
124 PMBUS_FAN_CONFIG_34
127 static const int pmbus_fan_command_registers[] = {
128 PMBUS_FAN_COMMAND_1,
129 PMBUS_FAN_COMMAND_2,
130 PMBUS_FAN_COMMAND_3,
131 PMBUS_FAN_COMMAND_4,
134 void pmbus_clear_cache(struct i2c_client *client)
136 struct pmbus_data *data = i2c_get_clientdata(client);
137 struct pmbus_sensor *sensor;
139 for (sensor = data->sensors; sensor; sensor = sensor->next)
140 sensor->data = -ENODATA;
142 EXPORT_SYMBOL_GPL(pmbus_clear_cache);
144 int pmbus_set_page(struct i2c_client *client, int page, int phase)
146 struct pmbus_data *data = i2c_get_clientdata(client);
147 int rv;
149 if (page < 0)
150 return 0;
152 if (!(data->info->func[page] & PMBUS_PAGE_VIRTUAL) &&
153 data->info->pages > 1 && page != data->currpage) {
154 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
155 if (rv < 0)
156 return rv;
158 rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
159 if (rv < 0)
160 return rv;
162 if (rv != page)
163 return -EIO;
165 data->currpage = page;
167 if (data->info->phases[page] && data->currphase != phase &&
168 !(data->info->func[page] & PMBUS_PHASE_VIRTUAL)) {
169 rv = i2c_smbus_write_byte_data(client, PMBUS_PHASE,
170 phase);
171 if (rv)
172 return rv;
174 data->currphase = phase;
176 return 0;
178 EXPORT_SYMBOL_GPL(pmbus_set_page);
180 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
182 int rv;
184 rv = pmbus_set_page(client, page, 0xff);
185 if (rv < 0)
186 return rv;
188 return i2c_smbus_write_byte(client, value);
190 EXPORT_SYMBOL_GPL(pmbus_write_byte);
193 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
194 * a device specific mapping function exists and calls it if necessary.
196 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
198 struct pmbus_data *data = i2c_get_clientdata(client);
199 const struct pmbus_driver_info *info = data->info;
200 int status;
202 if (info->write_byte) {
203 status = info->write_byte(client, page, value);
204 if (status != -ENODATA)
205 return status;
207 return pmbus_write_byte(client, page, value);
210 int pmbus_write_word_data(struct i2c_client *client, int page, u8 reg,
211 u16 word)
213 int rv;
215 rv = pmbus_set_page(client, page, 0xff);
216 if (rv < 0)
217 return rv;
219 return i2c_smbus_write_word_data(client, reg, word);
221 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
224 static int pmbus_write_virt_reg(struct i2c_client *client, int page, int reg,
225 u16 word)
227 int bit;
228 int id;
229 int rv;
231 switch (reg) {
232 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4:
233 id = reg - PMBUS_VIRT_FAN_TARGET_1;
234 bit = pmbus_fan_rpm_mask[id];
235 rv = pmbus_update_fan(client, page, id, bit, bit, word);
236 break;
237 default:
238 rv = -ENXIO;
239 break;
242 return rv;
246 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
247 * a device specific mapping function exists and calls it if necessary.
249 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
250 u16 word)
252 struct pmbus_data *data = i2c_get_clientdata(client);
253 const struct pmbus_driver_info *info = data->info;
254 int status;
256 if (info->write_word_data) {
257 status = info->write_word_data(client, page, reg, word);
258 if (status != -ENODATA)
259 return status;
262 if (reg >= PMBUS_VIRT_BASE)
263 return pmbus_write_virt_reg(client, page, reg, word);
265 return pmbus_write_word_data(client, page, reg, word);
268 int pmbus_update_fan(struct i2c_client *client, int page, int id,
269 u8 config, u8 mask, u16 command)
271 int from;
272 int rv;
273 u8 to;
275 from = pmbus_read_byte_data(client, page,
276 pmbus_fan_config_registers[id]);
277 if (from < 0)
278 return from;
280 to = (from & ~mask) | (config & mask);
281 if (to != from) {
282 rv = pmbus_write_byte_data(client, page,
283 pmbus_fan_config_registers[id], to);
284 if (rv < 0)
285 return rv;
288 return _pmbus_write_word_data(client, page,
289 pmbus_fan_command_registers[id], command);
291 EXPORT_SYMBOL_GPL(pmbus_update_fan);
293 int pmbus_read_word_data(struct i2c_client *client, int page, int phase, u8 reg)
295 int rv;
297 rv = pmbus_set_page(client, page, phase);
298 if (rv < 0)
299 return rv;
301 return i2c_smbus_read_word_data(client, reg);
303 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
305 static int pmbus_read_virt_reg(struct i2c_client *client, int page, int reg)
307 int rv;
308 int id;
310 switch (reg) {
311 case PMBUS_VIRT_FAN_TARGET_1 ... PMBUS_VIRT_FAN_TARGET_4:
312 id = reg - PMBUS_VIRT_FAN_TARGET_1;
313 rv = pmbus_get_fan_rate_device(client, page, id, rpm);
314 break;
315 default:
316 rv = -ENXIO;
317 break;
320 return rv;
324 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
325 * a device specific mapping function exists and calls it if necessary.
327 static int _pmbus_read_word_data(struct i2c_client *client, int page,
328 int phase, int reg)
330 struct pmbus_data *data = i2c_get_clientdata(client);
331 const struct pmbus_driver_info *info = data->info;
332 int status;
334 if (info->read_word_data) {
335 status = info->read_word_data(client, page, phase, reg);
336 if (status != -ENODATA)
337 return status;
340 if (reg >= PMBUS_VIRT_BASE)
341 return pmbus_read_virt_reg(client, page, reg);
343 return pmbus_read_word_data(client, page, phase, reg);
346 /* Same as above, but without phase parameter, for use in check functions */
347 static int __pmbus_read_word_data(struct i2c_client *client, int page, int reg)
349 return _pmbus_read_word_data(client, page, 0xff, reg);
352 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
354 int rv;
356 rv = pmbus_set_page(client, page, 0xff);
357 if (rv < 0)
358 return rv;
360 return i2c_smbus_read_byte_data(client, reg);
362 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
364 int pmbus_write_byte_data(struct i2c_client *client, int page, u8 reg, u8 value)
366 int rv;
368 rv = pmbus_set_page(client, page, 0xff);
369 if (rv < 0)
370 return rv;
372 return i2c_smbus_write_byte_data(client, reg, value);
374 EXPORT_SYMBOL_GPL(pmbus_write_byte_data);
376 int pmbus_update_byte_data(struct i2c_client *client, int page, u8 reg,
377 u8 mask, u8 value)
379 unsigned int tmp;
380 int rv;
382 rv = pmbus_read_byte_data(client, page, reg);
383 if (rv < 0)
384 return rv;
386 tmp = (rv & ~mask) | (value & mask);
388 if (tmp != rv)
389 rv = pmbus_write_byte_data(client, page, reg, tmp);
391 return rv;
393 EXPORT_SYMBOL_GPL(pmbus_update_byte_data);
396 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
397 * a device specific mapping function exists and calls it if necessary.
399 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
401 struct pmbus_data *data = i2c_get_clientdata(client);
402 const struct pmbus_driver_info *info = data->info;
403 int status;
405 if (info->read_byte_data) {
406 status = info->read_byte_data(client, page, reg);
407 if (status != -ENODATA)
408 return status;
410 return pmbus_read_byte_data(client, page, reg);
413 static struct pmbus_sensor *pmbus_find_sensor(struct pmbus_data *data, int page,
414 int reg)
416 struct pmbus_sensor *sensor;
418 for (sensor = data->sensors; sensor; sensor = sensor->next) {
419 if (sensor->page == page && sensor->reg == reg)
420 return sensor;
423 return ERR_PTR(-EINVAL);
426 static int pmbus_get_fan_rate(struct i2c_client *client, int page, int id,
427 enum pmbus_fan_mode mode,
428 bool from_cache)
430 struct pmbus_data *data = i2c_get_clientdata(client);
431 bool want_rpm, have_rpm;
432 struct pmbus_sensor *s;
433 int config;
434 int reg;
436 want_rpm = (mode == rpm);
438 if (from_cache) {
439 reg = want_rpm ? PMBUS_VIRT_FAN_TARGET_1 : PMBUS_VIRT_PWM_1;
440 s = pmbus_find_sensor(data, page, reg + id);
441 if (IS_ERR(s))
442 return PTR_ERR(s);
444 return s->data;
447 config = pmbus_read_byte_data(client, page,
448 pmbus_fan_config_registers[id]);
449 if (config < 0)
450 return config;
452 have_rpm = !!(config & pmbus_fan_rpm_mask[id]);
453 if (want_rpm == have_rpm)
454 return pmbus_read_word_data(client, page, 0xff,
455 pmbus_fan_command_registers[id]);
457 /* Can't sensibly map between RPM and PWM, just return zero */
458 return 0;
461 int pmbus_get_fan_rate_device(struct i2c_client *client, int page, int id,
462 enum pmbus_fan_mode mode)
464 return pmbus_get_fan_rate(client, page, id, mode, false);
466 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_device);
468 int pmbus_get_fan_rate_cached(struct i2c_client *client, int page, int id,
469 enum pmbus_fan_mode mode)
471 return pmbus_get_fan_rate(client, page, id, mode, true);
473 EXPORT_SYMBOL_GPL(pmbus_get_fan_rate_cached);
475 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
477 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
480 void pmbus_clear_faults(struct i2c_client *client)
482 struct pmbus_data *data = i2c_get_clientdata(client);
483 int i;
485 for (i = 0; i < data->info->pages; i++)
486 pmbus_clear_fault_page(client, i);
488 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
490 static int pmbus_check_status_cml(struct i2c_client *client)
492 struct pmbus_data *data = i2c_get_clientdata(client);
493 int status, status2;
495 status = data->read_status(client, -1);
496 if (status < 0 || (status & PB_STATUS_CML)) {
497 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
498 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
499 return -EIO;
501 return 0;
504 static bool pmbus_check_register(struct i2c_client *client,
505 int (*func)(struct i2c_client *client,
506 int page, int reg),
507 int page, int reg)
509 int rv;
510 struct pmbus_data *data = i2c_get_clientdata(client);
512 rv = func(client, page, reg);
513 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
514 rv = pmbus_check_status_cml(client);
515 pmbus_clear_fault_page(client, -1);
516 return rv >= 0;
519 static bool pmbus_check_status_register(struct i2c_client *client, int page)
521 int status;
522 struct pmbus_data *data = i2c_get_clientdata(client);
524 status = data->read_status(client, page);
525 if (status >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK) &&
526 (status & PB_STATUS_CML)) {
527 status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
528 if (status < 0 || (status & PB_CML_FAULT_INVALID_COMMAND))
529 status = -EIO;
532 pmbus_clear_fault_page(client, -1);
533 return status >= 0;
536 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
538 return pmbus_check_register(client, _pmbus_read_byte_data, page, reg);
540 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
542 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
544 return pmbus_check_register(client, __pmbus_read_word_data, page, reg);
546 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
548 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
550 struct pmbus_data *data = i2c_get_clientdata(client);
552 return data->info;
554 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
556 static int pmbus_get_status(struct i2c_client *client, int page, int reg)
558 struct pmbus_data *data = i2c_get_clientdata(client);
559 int status;
561 switch (reg) {
562 case PMBUS_STATUS_WORD:
563 status = data->read_status(client, page);
564 break;
565 default:
566 status = _pmbus_read_byte_data(client, page, reg);
567 break;
569 if (status < 0)
570 pmbus_clear_faults(client);
571 return status;
574 static void pmbus_update_sensor_data(struct i2c_client *client, struct pmbus_sensor *sensor)
576 if (sensor->data < 0 || sensor->update)
577 sensor->data = _pmbus_read_word_data(client, sensor->page,
578 sensor->phase, sensor->reg);
582 * Convert linear sensor values to milli- or micro-units
583 * depending on sensor type.
585 static s64 pmbus_reg2data_linear(struct pmbus_data *data,
586 struct pmbus_sensor *sensor)
588 s16 exponent;
589 s32 mantissa;
590 s64 val;
592 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
593 exponent = data->exponent[sensor->page];
594 mantissa = (u16) sensor->data;
595 } else { /* LINEAR11 */
596 exponent = ((s16)sensor->data) >> 11;
597 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
600 val = mantissa;
602 /* scale result to milli-units for all sensors except fans */
603 if (sensor->class != PSC_FAN)
604 val = val * 1000LL;
606 /* scale result to micro-units for power sensors */
607 if (sensor->class == PSC_POWER)
608 val = val * 1000LL;
610 if (exponent >= 0)
611 val <<= exponent;
612 else
613 val >>= -exponent;
615 return val;
619 * Convert direct sensor values to milli- or micro-units
620 * depending on sensor type.
622 static s64 pmbus_reg2data_direct(struct pmbus_data *data,
623 struct pmbus_sensor *sensor)
625 s64 b, val = (s16)sensor->data;
626 s32 m, R;
628 m = data->info->m[sensor->class];
629 b = data->info->b[sensor->class];
630 R = data->info->R[sensor->class];
632 if (m == 0)
633 return 0;
635 /* X = 1/m * (Y * 10^-R - b) */
636 R = -R;
637 /* scale result to milli-units for everything but fans */
638 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
639 R += 3;
640 b *= 1000;
643 /* scale result to micro-units for power sensors */
644 if (sensor->class == PSC_POWER) {
645 R += 3;
646 b *= 1000;
649 while (R > 0) {
650 val *= 10;
651 R--;
653 while (R < 0) {
654 val = div_s64(val + 5LL, 10L); /* round closest */
655 R++;
658 val = div_s64(val - b, m);
659 return val;
663 * Convert VID sensor values to milli- or micro-units
664 * depending on sensor type.
666 static s64 pmbus_reg2data_vid(struct pmbus_data *data,
667 struct pmbus_sensor *sensor)
669 long val = sensor->data;
670 long rv = 0;
672 switch (data->info->vrm_version[sensor->page]) {
673 case vr11:
674 if (val >= 0x02 && val <= 0xb2)
675 rv = DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
676 break;
677 case vr12:
678 if (val >= 0x01)
679 rv = 250 + (val - 1) * 5;
680 break;
681 case vr13:
682 if (val >= 0x01)
683 rv = 500 + (val - 1) * 10;
684 break;
685 case imvp9:
686 if (val >= 0x01)
687 rv = 200 + (val - 1) * 10;
688 break;
689 case amd625mv:
690 if (val >= 0x0 && val <= 0xd8)
691 rv = DIV_ROUND_CLOSEST(155000 - val * 625, 100);
692 break;
694 return rv;
697 static s64 pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
699 s64 val;
701 if (!sensor->convert)
702 return sensor->data;
704 switch (data->info->format[sensor->class]) {
705 case direct:
706 val = pmbus_reg2data_direct(data, sensor);
707 break;
708 case vid:
709 val = pmbus_reg2data_vid(data, sensor);
710 break;
711 case linear:
712 default:
713 val = pmbus_reg2data_linear(data, sensor);
714 break;
716 return val;
719 #define MAX_MANTISSA (1023 * 1000)
720 #define MIN_MANTISSA (511 * 1000)
722 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
723 struct pmbus_sensor *sensor, s64 val)
725 s16 exponent = 0, mantissa;
726 bool negative = false;
728 /* simple case */
729 if (val == 0)
730 return 0;
732 if (sensor->class == PSC_VOLTAGE_OUT) {
733 /* LINEAR16 does not support negative voltages */
734 if (val < 0)
735 return 0;
738 * For a static exponents, we don't have a choice
739 * but to adjust the value to it.
741 if (data->exponent[sensor->page] < 0)
742 val <<= -data->exponent[sensor->page];
743 else
744 val >>= data->exponent[sensor->page];
745 val = DIV_ROUND_CLOSEST_ULL(val, 1000);
746 return clamp_val(val, 0, 0xffff);
749 if (val < 0) {
750 negative = true;
751 val = -val;
754 /* Power is in uW. Convert to mW before converting. */
755 if (sensor->class == PSC_POWER)
756 val = DIV_ROUND_CLOSEST_ULL(val, 1000);
759 * For simplicity, convert fan data to milli-units
760 * before calculating the exponent.
762 if (sensor->class == PSC_FAN)
763 val = val * 1000LL;
765 /* Reduce large mantissa until it fits into 10 bit */
766 while (val >= MAX_MANTISSA && exponent < 15) {
767 exponent++;
768 val >>= 1;
770 /* Increase small mantissa to improve precision */
771 while (val < MIN_MANTISSA && exponent > -15) {
772 exponent--;
773 val <<= 1;
776 /* Convert mantissa from milli-units to units */
777 mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);
779 /* restore sign */
780 if (negative)
781 mantissa = -mantissa;
783 /* Convert to 5 bit exponent, 11 bit mantissa */
784 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
787 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
788 struct pmbus_sensor *sensor, s64 val)
790 s64 b;
791 s32 m, R;
793 m = data->info->m[sensor->class];
794 b = data->info->b[sensor->class];
795 R = data->info->R[sensor->class];
797 /* Power is in uW. Adjust R and b. */
798 if (sensor->class == PSC_POWER) {
799 R -= 3;
800 b *= 1000;
803 /* Calculate Y = (m * X + b) * 10^R */
804 if (!(sensor->class == PSC_FAN || sensor->class == PSC_PWM)) {
805 R -= 3; /* Adjust R and b for data in milli-units */
806 b *= 1000;
808 val = val * m + b;
810 while (R > 0) {
811 val *= 10;
812 R--;
814 while (R < 0) {
815 val = div_s64(val + 5LL, 10L); /* round closest */
816 R++;
819 return (u16)clamp_val(val, S16_MIN, S16_MAX);
822 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
823 struct pmbus_sensor *sensor, s64 val)
825 val = clamp_val(val, 500, 1600);
827 return 2 + DIV_ROUND_CLOSEST_ULL((1600LL - val) * 100LL, 625);
830 static u16 pmbus_data2reg(struct pmbus_data *data,
831 struct pmbus_sensor *sensor, s64 val)
833 u16 regval;
835 if (!sensor->convert)
836 return val;
838 switch (data->info->format[sensor->class]) {
839 case direct:
840 regval = pmbus_data2reg_direct(data, sensor, val);
841 break;
842 case vid:
843 regval = pmbus_data2reg_vid(data, sensor, val);
844 break;
845 case linear:
846 default:
847 regval = pmbus_data2reg_linear(data, sensor, val);
848 break;
850 return regval;
854 * Return boolean calculated from converted data.
855 * <index> defines a status register index and mask.
856 * The mask is in the lower 8 bits, the register index is in bits 8..23.
858 * The associated pmbus_boolean structure contains optional pointers to two
859 * sensor attributes. If specified, those attributes are compared against each
860 * other to determine if a limit has been exceeded.
862 * If the sensor attribute pointers are NULL, the function returns true if
863 * (status[reg] & mask) is true.
865 * If sensor attribute pointers are provided, a comparison against a specified
866 * limit has to be performed to determine the boolean result.
867 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
868 * sensor values referenced by sensor attribute pointers s1 and s2).
870 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
871 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
873 * If a negative value is stored in any of the referenced registers, this value
874 * reflects an error code which will be returned.
876 static int pmbus_get_boolean(struct i2c_client *client, struct pmbus_boolean *b,
877 int index)
879 struct pmbus_data *data = i2c_get_clientdata(client);
880 struct pmbus_sensor *s1 = b->s1;
881 struct pmbus_sensor *s2 = b->s2;
882 u16 mask = pb_index_to_mask(index);
883 u8 page = pb_index_to_page(index);
884 u16 reg = pb_index_to_reg(index);
885 int ret, status;
886 u16 regval;
888 mutex_lock(&data->update_lock);
889 status = pmbus_get_status(client, page, reg);
890 if (status < 0) {
891 ret = status;
892 goto unlock;
895 if (s1)
896 pmbus_update_sensor_data(client, s1);
897 if (s2)
898 pmbus_update_sensor_data(client, s2);
900 regval = status & mask;
901 if (s1 && s2) {
902 s64 v1, v2;
904 if (s1->data < 0) {
905 ret = s1->data;
906 goto unlock;
908 if (s2->data < 0) {
909 ret = s2->data;
910 goto unlock;
913 v1 = pmbus_reg2data(data, s1);
914 v2 = pmbus_reg2data(data, s2);
915 ret = !!(regval && v1 >= v2);
916 } else {
917 ret = !!regval;
919 unlock:
920 mutex_unlock(&data->update_lock);
921 return ret;
924 static ssize_t pmbus_show_boolean(struct device *dev,
925 struct device_attribute *da, char *buf)
927 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
928 struct pmbus_boolean *boolean = to_pmbus_boolean(attr);
929 struct i2c_client *client = to_i2c_client(dev->parent);
930 int val;
932 val = pmbus_get_boolean(client, boolean, attr->index);
933 if (val < 0)
934 return val;
935 return snprintf(buf, PAGE_SIZE, "%d\n", val);
938 static ssize_t pmbus_show_sensor(struct device *dev,
939 struct device_attribute *devattr, char *buf)
941 struct i2c_client *client = to_i2c_client(dev->parent);
942 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
943 struct pmbus_data *data = i2c_get_clientdata(client);
944 ssize_t ret;
946 mutex_lock(&data->update_lock);
947 pmbus_update_sensor_data(client, sensor);
948 if (sensor->data < 0)
949 ret = sensor->data;
950 else
951 ret = snprintf(buf, PAGE_SIZE, "%lld\n", pmbus_reg2data(data, sensor));
952 mutex_unlock(&data->update_lock);
953 return ret;
956 static ssize_t pmbus_set_sensor(struct device *dev,
957 struct device_attribute *devattr,
958 const char *buf, size_t count)
960 struct i2c_client *client = to_i2c_client(dev->parent);
961 struct pmbus_data *data = i2c_get_clientdata(client);
962 struct pmbus_sensor *sensor = to_pmbus_sensor(devattr);
963 ssize_t rv = count;
964 s64 val;
965 int ret;
966 u16 regval;
968 if (kstrtos64(buf, 10, &val) < 0)
969 return -EINVAL;
971 mutex_lock(&data->update_lock);
972 regval = pmbus_data2reg(data, sensor, val);
973 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
974 if (ret < 0)
975 rv = ret;
976 else
977 sensor->data = regval;
978 mutex_unlock(&data->update_lock);
979 return rv;
982 static ssize_t pmbus_show_label(struct device *dev,
983 struct device_attribute *da, char *buf)
985 struct pmbus_label *label = to_pmbus_label(da);
987 return snprintf(buf, PAGE_SIZE, "%s\n", label->label);
990 static int pmbus_add_attribute(struct pmbus_data *data, struct attribute *attr)
992 if (data->num_attributes >= data->max_attributes - 1) {
993 int new_max_attrs = data->max_attributes + PMBUS_ATTR_ALLOC_SIZE;
994 void *new_attrs = devm_krealloc(data->dev, data->group.attrs,
995 new_max_attrs * sizeof(void *),
996 GFP_KERNEL);
997 if (!new_attrs)
998 return -ENOMEM;
999 data->group.attrs = new_attrs;
1000 data->max_attributes = new_max_attrs;
1003 data->group.attrs[data->num_attributes++] = attr;
1004 data->group.attrs[data->num_attributes] = NULL;
1005 return 0;
1008 static void pmbus_dev_attr_init(struct device_attribute *dev_attr,
1009 const char *name,
1010 umode_t mode,
1011 ssize_t (*show)(struct device *dev,
1012 struct device_attribute *attr,
1013 char *buf),
1014 ssize_t (*store)(struct device *dev,
1015 struct device_attribute *attr,
1016 const char *buf, size_t count))
1018 sysfs_attr_init(&dev_attr->attr);
1019 dev_attr->attr.name = name;
1020 dev_attr->attr.mode = mode;
1021 dev_attr->show = show;
1022 dev_attr->store = store;
1025 static void pmbus_attr_init(struct sensor_device_attribute *a,
1026 const char *name,
1027 umode_t mode,
1028 ssize_t (*show)(struct device *dev,
1029 struct device_attribute *attr,
1030 char *buf),
1031 ssize_t (*store)(struct device *dev,
1032 struct device_attribute *attr,
1033 const char *buf, size_t count),
1034 int idx)
1036 pmbus_dev_attr_init(&a->dev_attr, name, mode, show, store);
1037 a->index = idx;
1040 static int pmbus_add_boolean(struct pmbus_data *data,
1041 const char *name, const char *type, int seq,
1042 struct pmbus_sensor *s1,
1043 struct pmbus_sensor *s2,
1044 u8 page, u16 reg, u16 mask)
1046 struct pmbus_boolean *boolean;
1047 struct sensor_device_attribute *a;
1049 if (WARN((s1 && !s2) || (!s1 && s2), "Bad s1/s2 parameters\n"))
1050 return -EINVAL;
1052 boolean = devm_kzalloc(data->dev, sizeof(*boolean), GFP_KERNEL);
1053 if (!boolean)
1054 return -ENOMEM;
1056 a = &boolean->attribute;
1058 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
1059 name, seq, type);
1060 boolean->s1 = s1;
1061 boolean->s2 = s2;
1062 pmbus_attr_init(a, boolean->name, 0444, pmbus_show_boolean, NULL,
1063 pb_reg_to_index(page, reg, mask));
1065 return pmbus_add_attribute(data, &a->dev_attr.attr);
1068 static struct pmbus_sensor *pmbus_add_sensor(struct pmbus_data *data,
1069 const char *name, const char *type,
1070 int seq, int page, int phase,
1071 int reg,
1072 enum pmbus_sensor_classes class,
1073 bool update, bool readonly,
1074 bool convert)
1076 struct pmbus_sensor *sensor;
1077 struct device_attribute *a;
1079 sensor = devm_kzalloc(data->dev, sizeof(*sensor), GFP_KERNEL);
1080 if (!sensor)
1081 return NULL;
1082 a = &sensor->attribute;
1084 if (type)
1085 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
1086 name, seq, type);
1087 else
1088 snprintf(sensor->name, sizeof(sensor->name), "%s%d",
1089 name, seq);
1091 if (data->flags & PMBUS_WRITE_PROTECTED)
1092 readonly = true;
1094 sensor->page = page;
1095 sensor->phase = phase;
1096 sensor->reg = reg;
1097 sensor->class = class;
1098 sensor->update = update;
1099 sensor->convert = convert;
1100 sensor->data = -ENODATA;
1101 pmbus_dev_attr_init(a, sensor->name,
1102 readonly ? 0444 : 0644,
1103 pmbus_show_sensor, pmbus_set_sensor);
1105 if (pmbus_add_attribute(data, &a->attr))
1106 return NULL;
1108 sensor->next = data->sensors;
1109 data->sensors = sensor;
1111 return sensor;
1114 static int pmbus_add_label(struct pmbus_data *data,
1115 const char *name, int seq,
1116 const char *lstring, int index, int phase)
1118 struct pmbus_label *label;
1119 struct device_attribute *a;
1121 label = devm_kzalloc(data->dev, sizeof(*label), GFP_KERNEL);
1122 if (!label)
1123 return -ENOMEM;
1125 a = &label->attribute;
1127 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
1128 if (!index) {
1129 if (phase == 0xff)
1130 strncpy(label->label, lstring,
1131 sizeof(label->label) - 1);
1132 else
1133 snprintf(label->label, sizeof(label->label), "%s.%d",
1134 lstring, phase);
1135 } else {
1136 if (phase == 0xff)
1137 snprintf(label->label, sizeof(label->label), "%s%d",
1138 lstring, index);
1139 else
1140 snprintf(label->label, sizeof(label->label), "%s%d.%d",
1141 lstring, index, phase);
1144 pmbus_dev_attr_init(a, label->name, 0444, pmbus_show_label, NULL);
1145 return pmbus_add_attribute(data, &a->attr);
1149 * Search for attributes. Allocate sensors, booleans, and labels as needed.
1153 * The pmbus_limit_attr structure describes a single limit attribute
1154 * and its associated alarm attribute.
1156 struct pmbus_limit_attr {
1157 u16 reg; /* Limit register */
1158 u16 sbit; /* Alarm attribute status bit */
1159 bool update; /* True if register needs updates */
1160 bool low; /* True if low limit; for limits with compare
1161 functions only */
1162 const char *attr; /* Attribute name */
1163 const char *alarm; /* Alarm attribute name */
1167 * The pmbus_sensor_attr structure describes one sensor attribute. This
1168 * description includes a reference to the associated limit attributes.
1170 struct pmbus_sensor_attr {
1171 u16 reg; /* sensor register */
1172 u16 gbit; /* generic status bit */
1173 u8 nlimit; /* # of limit registers */
1174 enum pmbus_sensor_classes class;/* sensor class */
1175 const char *label; /* sensor label */
1176 bool paged; /* true if paged sensor */
1177 bool update; /* true if update needed */
1178 bool compare; /* true if compare function needed */
1179 u32 func; /* sensor mask */
1180 u32 sfunc; /* sensor status mask */
1181 int sreg; /* status register */
1182 const struct pmbus_limit_attr *limit;/* limit registers */
1186 * Add a set of limit attributes and, if supported, the associated
1187 * alarm attributes.
1188 * returns 0 if no alarm register found, 1 if an alarm register was found,
1189 * < 0 on errors.
1191 static int pmbus_add_limit_attrs(struct i2c_client *client,
1192 struct pmbus_data *data,
1193 const struct pmbus_driver_info *info,
1194 const char *name, int index, int page,
1195 struct pmbus_sensor *base,
1196 const struct pmbus_sensor_attr *attr)
1198 const struct pmbus_limit_attr *l = attr->limit;
1199 int nlimit = attr->nlimit;
1200 int have_alarm = 0;
1201 int i, ret;
1202 struct pmbus_sensor *curr;
1204 for (i = 0; i < nlimit; i++) {
1205 if (pmbus_check_word_register(client, page, l->reg)) {
1206 curr = pmbus_add_sensor(data, name, l->attr, index,
1207 page, 0xff, l->reg, attr->class,
1208 attr->update || l->update,
1209 false, true);
1210 if (!curr)
1211 return -ENOMEM;
1212 if (l->sbit && (info->func[page] & attr->sfunc)) {
1213 ret = pmbus_add_boolean(data, name,
1214 l->alarm, index,
1215 attr->compare ? l->low ? curr : base
1216 : NULL,
1217 attr->compare ? l->low ? base : curr
1218 : NULL,
1219 page, attr->sreg, l->sbit);
1220 if (ret)
1221 return ret;
1222 have_alarm = 1;
1225 l++;
1227 return have_alarm;
1230 static int pmbus_add_sensor_attrs_one(struct i2c_client *client,
1231 struct pmbus_data *data,
1232 const struct pmbus_driver_info *info,
1233 const char *name,
1234 int index, int page, int phase,
1235 const struct pmbus_sensor_attr *attr,
1236 bool paged)
1238 struct pmbus_sensor *base;
1239 bool upper = !!(attr->gbit & 0xff00); /* need to check STATUS_WORD */
1240 int ret;
1242 if (attr->label) {
1243 ret = pmbus_add_label(data, name, index, attr->label,
1244 paged ? page + 1 : 0, phase);
1245 if (ret)
1246 return ret;
1248 base = pmbus_add_sensor(data, name, "input", index, page, phase,
1249 attr->reg, attr->class, true, true, true);
1250 if (!base)
1251 return -ENOMEM;
1252 /* No limit and alarm attributes for phase specific sensors */
1253 if (attr->sfunc && phase == 0xff) {
1254 ret = pmbus_add_limit_attrs(client, data, info, name,
1255 index, page, base, attr);
1256 if (ret < 0)
1257 return ret;
1259 * Add generic alarm attribute only if there are no individual
1260 * alarm attributes, if there is a global alarm bit, and if
1261 * the generic status register (word or byte, depending on
1262 * which global bit is set) for this page is accessible.
1264 if (!ret && attr->gbit &&
1265 (!upper || (upper && data->has_status_word)) &&
1266 pmbus_check_status_register(client, page)) {
1267 ret = pmbus_add_boolean(data, name, "alarm", index,
1268 NULL, NULL,
1269 page, PMBUS_STATUS_WORD,
1270 attr->gbit);
1271 if (ret)
1272 return ret;
1275 return 0;
1278 static bool pmbus_sensor_is_paged(const struct pmbus_driver_info *info,
1279 const struct pmbus_sensor_attr *attr)
1281 int p;
1283 if (attr->paged)
1284 return true;
1287 * Some attributes may be present on more than one page despite
1288 * not being marked with the paged attribute. If that is the case,
1289 * then treat the sensor as being paged and add the page suffix to the
1290 * attribute name.
1291 * We don't just add the paged attribute to all such attributes, in
1292 * order to maintain the un-suffixed labels in the case where the
1293 * attribute is only on page 0.
1295 for (p = 1; p < info->pages; p++) {
1296 if (info->func[p] & attr->func)
1297 return true;
1299 return false;
1302 static int pmbus_add_sensor_attrs(struct i2c_client *client,
1303 struct pmbus_data *data,
1304 const char *name,
1305 const struct pmbus_sensor_attr *attrs,
1306 int nattrs)
1308 const struct pmbus_driver_info *info = data->info;
1309 int index, i;
1310 int ret;
1312 index = 1;
1313 for (i = 0; i < nattrs; i++) {
1314 int page, pages;
1315 bool paged = pmbus_sensor_is_paged(info, attrs);
1317 pages = paged ? info->pages : 1;
1318 for (page = 0; page < pages; page++) {
1319 if (!(info->func[page] & attrs->func))
1320 continue;
1321 ret = pmbus_add_sensor_attrs_one(client, data, info,
1322 name, index, page,
1323 0xff, attrs, paged);
1324 if (ret)
1325 return ret;
1326 index++;
1327 if (info->phases[page]) {
1328 int phase;
1330 for (phase = 0; phase < info->phases[page];
1331 phase++) {
1332 if (!(info->pfunc[phase] & attrs->func))
1333 continue;
1334 ret = pmbus_add_sensor_attrs_one(client,
1335 data, info, name, index, page,
1336 phase, attrs, paged);
1337 if (ret)
1338 return ret;
1339 index++;
1343 attrs++;
1345 return 0;
1348 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1350 .reg = PMBUS_VIN_UV_WARN_LIMIT,
1351 .attr = "min",
1352 .alarm = "min_alarm",
1353 .sbit = PB_VOLTAGE_UV_WARNING,
1354 }, {
1355 .reg = PMBUS_VIN_UV_FAULT_LIMIT,
1356 .attr = "lcrit",
1357 .alarm = "lcrit_alarm",
1358 .sbit = PB_VOLTAGE_UV_FAULT,
1359 }, {
1360 .reg = PMBUS_VIN_OV_WARN_LIMIT,
1361 .attr = "max",
1362 .alarm = "max_alarm",
1363 .sbit = PB_VOLTAGE_OV_WARNING,
1364 }, {
1365 .reg = PMBUS_VIN_OV_FAULT_LIMIT,
1366 .attr = "crit",
1367 .alarm = "crit_alarm",
1368 .sbit = PB_VOLTAGE_OV_FAULT,
1369 }, {
1370 .reg = PMBUS_VIRT_READ_VIN_AVG,
1371 .update = true,
1372 .attr = "average",
1373 }, {
1374 .reg = PMBUS_VIRT_READ_VIN_MIN,
1375 .update = true,
1376 .attr = "lowest",
1377 }, {
1378 .reg = PMBUS_VIRT_READ_VIN_MAX,
1379 .update = true,
1380 .attr = "highest",
1381 }, {
1382 .reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1383 .attr = "reset_history",
1384 }, {
1385 .reg = PMBUS_MFR_VIN_MIN,
1386 .attr = "rated_min",
1387 }, {
1388 .reg = PMBUS_MFR_VIN_MAX,
1389 .attr = "rated_max",
1393 static const struct pmbus_limit_attr vmon_limit_attrs[] = {
1395 .reg = PMBUS_VIRT_VMON_UV_WARN_LIMIT,
1396 .attr = "min",
1397 .alarm = "min_alarm",
1398 .sbit = PB_VOLTAGE_UV_WARNING,
1399 }, {
1400 .reg = PMBUS_VIRT_VMON_UV_FAULT_LIMIT,
1401 .attr = "lcrit",
1402 .alarm = "lcrit_alarm",
1403 .sbit = PB_VOLTAGE_UV_FAULT,
1404 }, {
1405 .reg = PMBUS_VIRT_VMON_OV_WARN_LIMIT,
1406 .attr = "max",
1407 .alarm = "max_alarm",
1408 .sbit = PB_VOLTAGE_OV_WARNING,
1409 }, {
1410 .reg = PMBUS_VIRT_VMON_OV_FAULT_LIMIT,
1411 .attr = "crit",
1412 .alarm = "crit_alarm",
1413 .sbit = PB_VOLTAGE_OV_FAULT,
1417 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1419 .reg = PMBUS_VOUT_UV_WARN_LIMIT,
1420 .attr = "min",
1421 .alarm = "min_alarm",
1422 .sbit = PB_VOLTAGE_UV_WARNING,
1423 }, {
1424 .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1425 .attr = "lcrit",
1426 .alarm = "lcrit_alarm",
1427 .sbit = PB_VOLTAGE_UV_FAULT,
1428 }, {
1429 .reg = PMBUS_VOUT_OV_WARN_LIMIT,
1430 .attr = "max",
1431 .alarm = "max_alarm",
1432 .sbit = PB_VOLTAGE_OV_WARNING,
1433 }, {
1434 .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1435 .attr = "crit",
1436 .alarm = "crit_alarm",
1437 .sbit = PB_VOLTAGE_OV_FAULT,
1438 }, {
1439 .reg = PMBUS_VIRT_READ_VOUT_AVG,
1440 .update = true,
1441 .attr = "average",
1442 }, {
1443 .reg = PMBUS_VIRT_READ_VOUT_MIN,
1444 .update = true,
1445 .attr = "lowest",
1446 }, {
1447 .reg = PMBUS_VIRT_READ_VOUT_MAX,
1448 .update = true,
1449 .attr = "highest",
1450 }, {
1451 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1452 .attr = "reset_history",
1453 }, {
1454 .reg = PMBUS_MFR_VOUT_MIN,
1455 .attr = "rated_min",
1456 }, {
1457 .reg = PMBUS_MFR_VOUT_MAX,
1458 .attr = "rated_max",
1462 static const struct pmbus_sensor_attr voltage_attributes[] = {
1464 .reg = PMBUS_READ_VIN,
1465 .class = PSC_VOLTAGE_IN,
1466 .label = "vin",
1467 .func = PMBUS_HAVE_VIN,
1468 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1469 .sreg = PMBUS_STATUS_INPUT,
1470 .gbit = PB_STATUS_VIN_UV,
1471 .limit = vin_limit_attrs,
1472 .nlimit = ARRAY_SIZE(vin_limit_attrs),
1473 }, {
1474 .reg = PMBUS_VIRT_READ_VMON,
1475 .class = PSC_VOLTAGE_IN,
1476 .label = "vmon",
1477 .func = PMBUS_HAVE_VMON,
1478 .sfunc = PMBUS_HAVE_STATUS_VMON,
1479 .sreg = PMBUS_VIRT_STATUS_VMON,
1480 .limit = vmon_limit_attrs,
1481 .nlimit = ARRAY_SIZE(vmon_limit_attrs),
1482 }, {
1483 .reg = PMBUS_READ_VCAP,
1484 .class = PSC_VOLTAGE_IN,
1485 .label = "vcap",
1486 .func = PMBUS_HAVE_VCAP,
1487 }, {
1488 .reg = PMBUS_READ_VOUT,
1489 .class = PSC_VOLTAGE_OUT,
1490 .label = "vout",
1491 .paged = true,
1492 .func = PMBUS_HAVE_VOUT,
1493 .sfunc = PMBUS_HAVE_STATUS_VOUT,
1494 .sreg = PMBUS_STATUS_VOUT,
1495 .gbit = PB_STATUS_VOUT_OV,
1496 .limit = vout_limit_attrs,
1497 .nlimit = ARRAY_SIZE(vout_limit_attrs),
1501 /* Current attributes */
1503 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1505 .reg = PMBUS_IIN_OC_WARN_LIMIT,
1506 .attr = "max",
1507 .alarm = "max_alarm",
1508 .sbit = PB_IIN_OC_WARNING,
1509 }, {
1510 .reg = PMBUS_IIN_OC_FAULT_LIMIT,
1511 .attr = "crit",
1512 .alarm = "crit_alarm",
1513 .sbit = PB_IIN_OC_FAULT,
1514 }, {
1515 .reg = PMBUS_VIRT_READ_IIN_AVG,
1516 .update = true,
1517 .attr = "average",
1518 }, {
1519 .reg = PMBUS_VIRT_READ_IIN_MIN,
1520 .update = true,
1521 .attr = "lowest",
1522 }, {
1523 .reg = PMBUS_VIRT_READ_IIN_MAX,
1524 .update = true,
1525 .attr = "highest",
1526 }, {
1527 .reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1528 .attr = "reset_history",
1529 }, {
1530 .reg = PMBUS_MFR_IIN_MAX,
1531 .attr = "rated_max",
1535 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1537 .reg = PMBUS_IOUT_OC_WARN_LIMIT,
1538 .attr = "max",
1539 .alarm = "max_alarm",
1540 .sbit = PB_IOUT_OC_WARNING,
1541 }, {
1542 .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1543 .attr = "lcrit",
1544 .alarm = "lcrit_alarm",
1545 .sbit = PB_IOUT_UC_FAULT,
1546 }, {
1547 .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1548 .attr = "crit",
1549 .alarm = "crit_alarm",
1550 .sbit = PB_IOUT_OC_FAULT,
1551 }, {
1552 .reg = PMBUS_VIRT_READ_IOUT_AVG,
1553 .update = true,
1554 .attr = "average",
1555 }, {
1556 .reg = PMBUS_VIRT_READ_IOUT_MIN,
1557 .update = true,
1558 .attr = "lowest",
1559 }, {
1560 .reg = PMBUS_VIRT_READ_IOUT_MAX,
1561 .update = true,
1562 .attr = "highest",
1563 }, {
1564 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1565 .attr = "reset_history",
1566 }, {
1567 .reg = PMBUS_MFR_IOUT_MAX,
1568 .attr = "rated_max",
1572 static const struct pmbus_sensor_attr current_attributes[] = {
1574 .reg = PMBUS_READ_IIN,
1575 .class = PSC_CURRENT_IN,
1576 .label = "iin",
1577 .func = PMBUS_HAVE_IIN,
1578 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1579 .sreg = PMBUS_STATUS_INPUT,
1580 .gbit = PB_STATUS_INPUT,
1581 .limit = iin_limit_attrs,
1582 .nlimit = ARRAY_SIZE(iin_limit_attrs),
1583 }, {
1584 .reg = PMBUS_READ_IOUT,
1585 .class = PSC_CURRENT_OUT,
1586 .label = "iout",
1587 .paged = true,
1588 .func = PMBUS_HAVE_IOUT,
1589 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1590 .sreg = PMBUS_STATUS_IOUT,
1591 .gbit = PB_STATUS_IOUT_OC,
1592 .limit = iout_limit_attrs,
1593 .nlimit = ARRAY_SIZE(iout_limit_attrs),
1597 /* Power attributes */
1599 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1601 .reg = PMBUS_PIN_OP_WARN_LIMIT,
1602 .attr = "max",
1603 .alarm = "alarm",
1604 .sbit = PB_PIN_OP_WARNING,
1605 }, {
1606 .reg = PMBUS_VIRT_READ_PIN_AVG,
1607 .update = true,
1608 .attr = "average",
1609 }, {
1610 .reg = PMBUS_VIRT_READ_PIN_MIN,
1611 .update = true,
1612 .attr = "input_lowest",
1613 }, {
1614 .reg = PMBUS_VIRT_READ_PIN_MAX,
1615 .update = true,
1616 .attr = "input_highest",
1617 }, {
1618 .reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1619 .attr = "reset_history",
1620 }, {
1621 .reg = PMBUS_MFR_PIN_MAX,
1622 .attr = "rated_max",
1626 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1628 .reg = PMBUS_POUT_MAX,
1629 .attr = "cap",
1630 .alarm = "cap_alarm",
1631 .sbit = PB_POWER_LIMITING,
1632 }, {
1633 .reg = PMBUS_POUT_OP_WARN_LIMIT,
1634 .attr = "max",
1635 .alarm = "max_alarm",
1636 .sbit = PB_POUT_OP_WARNING,
1637 }, {
1638 .reg = PMBUS_POUT_OP_FAULT_LIMIT,
1639 .attr = "crit",
1640 .alarm = "crit_alarm",
1641 .sbit = PB_POUT_OP_FAULT,
1642 }, {
1643 .reg = PMBUS_VIRT_READ_POUT_AVG,
1644 .update = true,
1645 .attr = "average",
1646 }, {
1647 .reg = PMBUS_VIRT_READ_POUT_MIN,
1648 .update = true,
1649 .attr = "input_lowest",
1650 }, {
1651 .reg = PMBUS_VIRT_READ_POUT_MAX,
1652 .update = true,
1653 .attr = "input_highest",
1654 }, {
1655 .reg = PMBUS_VIRT_RESET_POUT_HISTORY,
1656 .attr = "reset_history",
1657 }, {
1658 .reg = PMBUS_MFR_POUT_MAX,
1659 .attr = "rated_max",
1663 static const struct pmbus_sensor_attr power_attributes[] = {
1665 .reg = PMBUS_READ_PIN,
1666 .class = PSC_POWER,
1667 .label = "pin",
1668 .func = PMBUS_HAVE_PIN,
1669 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1670 .sreg = PMBUS_STATUS_INPUT,
1671 .gbit = PB_STATUS_INPUT,
1672 .limit = pin_limit_attrs,
1673 .nlimit = ARRAY_SIZE(pin_limit_attrs),
1674 }, {
1675 .reg = PMBUS_READ_POUT,
1676 .class = PSC_POWER,
1677 .label = "pout",
1678 .paged = true,
1679 .func = PMBUS_HAVE_POUT,
1680 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1681 .sreg = PMBUS_STATUS_IOUT,
1682 .limit = pout_limit_attrs,
1683 .nlimit = ARRAY_SIZE(pout_limit_attrs),
1687 /* Temperature atributes */
1689 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1691 .reg = PMBUS_UT_WARN_LIMIT,
1692 .low = true,
1693 .attr = "min",
1694 .alarm = "min_alarm",
1695 .sbit = PB_TEMP_UT_WARNING,
1696 }, {
1697 .reg = PMBUS_UT_FAULT_LIMIT,
1698 .low = true,
1699 .attr = "lcrit",
1700 .alarm = "lcrit_alarm",
1701 .sbit = PB_TEMP_UT_FAULT,
1702 }, {
1703 .reg = PMBUS_OT_WARN_LIMIT,
1704 .attr = "max",
1705 .alarm = "max_alarm",
1706 .sbit = PB_TEMP_OT_WARNING,
1707 }, {
1708 .reg = PMBUS_OT_FAULT_LIMIT,
1709 .attr = "crit",
1710 .alarm = "crit_alarm",
1711 .sbit = PB_TEMP_OT_FAULT,
1712 }, {
1713 .reg = PMBUS_VIRT_READ_TEMP_MIN,
1714 .attr = "lowest",
1715 }, {
1716 .reg = PMBUS_VIRT_READ_TEMP_AVG,
1717 .attr = "average",
1718 }, {
1719 .reg = PMBUS_VIRT_READ_TEMP_MAX,
1720 .attr = "highest",
1721 }, {
1722 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1723 .attr = "reset_history",
1724 }, {
1725 .reg = PMBUS_MFR_MAX_TEMP_1,
1726 .attr = "rated_max",
1730 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1732 .reg = PMBUS_UT_WARN_LIMIT,
1733 .low = true,
1734 .attr = "min",
1735 .alarm = "min_alarm",
1736 .sbit = PB_TEMP_UT_WARNING,
1737 }, {
1738 .reg = PMBUS_UT_FAULT_LIMIT,
1739 .low = true,
1740 .attr = "lcrit",
1741 .alarm = "lcrit_alarm",
1742 .sbit = PB_TEMP_UT_FAULT,
1743 }, {
1744 .reg = PMBUS_OT_WARN_LIMIT,
1745 .attr = "max",
1746 .alarm = "max_alarm",
1747 .sbit = PB_TEMP_OT_WARNING,
1748 }, {
1749 .reg = PMBUS_OT_FAULT_LIMIT,
1750 .attr = "crit",
1751 .alarm = "crit_alarm",
1752 .sbit = PB_TEMP_OT_FAULT,
1753 }, {
1754 .reg = PMBUS_VIRT_READ_TEMP2_MIN,
1755 .attr = "lowest",
1756 }, {
1757 .reg = PMBUS_VIRT_READ_TEMP2_AVG,
1758 .attr = "average",
1759 }, {
1760 .reg = PMBUS_VIRT_READ_TEMP2_MAX,
1761 .attr = "highest",
1762 }, {
1763 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1764 .attr = "reset_history",
1765 }, {
1766 .reg = PMBUS_MFR_MAX_TEMP_2,
1767 .attr = "rated_max",
1771 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1773 .reg = PMBUS_UT_WARN_LIMIT,
1774 .low = true,
1775 .attr = "min",
1776 .alarm = "min_alarm",
1777 .sbit = PB_TEMP_UT_WARNING,
1778 }, {
1779 .reg = PMBUS_UT_FAULT_LIMIT,
1780 .low = true,
1781 .attr = "lcrit",
1782 .alarm = "lcrit_alarm",
1783 .sbit = PB_TEMP_UT_FAULT,
1784 }, {
1785 .reg = PMBUS_OT_WARN_LIMIT,
1786 .attr = "max",
1787 .alarm = "max_alarm",
1788 .sbit = PB_TEMP_OT_WARNING,
1789 }, {
1790 .reg = PMBUS_OT_FAULT_LIMIT,
1791 .attr = "crit",
1792 .alarm = "crit_alarm",
1793 .sbit = PB_TEMP_OT_FAULT,
1794 }, {
1795 .reg = PMBUS_MFR_MAX_TEMP_3,
1796 .attr = "rated_max",
1800 static const struct pmbus_sensor_attr temp_attributes[] = {
1802 .reg = PMBUS_READ_TEMPERATURE_1,
1803 .class = PSC_TEMPERATURE,
1804 .paged = true,
1805 .update = true,
1806 .compare = true,
1807 .func = PMBUS_HAVE_TEMP,
1808 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1809 .sreg = PMBUS_STATUS_TEMPERATURE,
1810 .gbit = PB_STATUS_TEMPERATURE,
1811 .limit = temp_limit_attrs,
1812 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1813 }, {
1814 .reg = PMBUS_READ_TEMPERATURE_2,
1815 .class = PSC_TEMPERATURE,
1816 .paged = true,
1817 .update = true,
1818 .compare = true,
1819 .func = PMBUS_HAVE_TEMP2,
1820 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1821 .sreg = PMBUS_STATUS_TEMPERATURE,
1822 .gbit = PB_STATUS_TEMPERATURE,
1823 .limit = temp_limit_attrs2,
1824 .nlimit = ARRAY_SIZE(temp_limit_attrs2),
1825 }, {
1826 .reg = PMBUS_READ_TEMPERATURE_3,
1827 .class = PSC_TEMPERATURE,
1828 .paged = true,
1829 .update = true,
1830 .compare = true,
1831 .func = PMBUS_HAVE_TEMP3,
1832 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1833 .sreg = PMBUS_STATUS_TEMPERATURE,
1834 .gbit = PB_STATUS_TEMPERATURE,
1835 .limit = temp_limit_attrs3,
1836 .nlimit = ARRAY_SIZE(temp_limit_attrs3),
1840 static const int pmbus_fan_registers[] = {
1841 PMBUS_READ_FAN_SPEED_1,
1842 PMBUS_READ_FAN_SPEED_2,
1843 PMBUS_READ_FAN_SPEED_3,
1844 PMBUS_READ_FAN_SPEED_4
1847 static const int pmbus_fan_status_registers[] = {
1848 PMBUS_STATUS_FAN_12,
1849 PMBUS_STATUS_FAN_12,
1850 PMBUS_STATUS_FAN_34,
1851 PMBUS_STATUS_FAN_34
1854 static const u32 pmbus_fan_flags[] = {
1855 PMBUS_HAVE_FAN12,
1856 PMBUS_HAVE_FAN12,
1857 PMBUS_HAVE_FAN34,
1858 PMBUS_HAVE_FAN34
1861 static const u32 pmbus_fan_status_flags[] = {
1862 PMBUS_HAVE_STATUS_FAN12,
1863 PMBUS_HAVE_STATUS_FAN12,
1864 PMBUS_HAVE_STATUS_FAN34,
1865 PMBUS_HAVE_STATUS_FAN34
1868 /* Fans */
1870 /* Precondition: FAN_CONFIG_x_y and FAN_COMMAND_x must exist for the fan ID */
1871 static int pmbus_add_fan_ctrl(struct i2c_client *client,
1872 struct pmbus_data *data, int index, int page, int id,
1873 u8 config)
1875 struct pmbus_sensor *sensor;
1877 sensor = pmbus_add_sensor(data, "fan", "target", index, page,
1878 0xff, PMBUS_VIRT_FAN_TARGET_1 + id, PSC_FAN,
1879 false, false, true);
1881 if (!sensor)
1882 return -ENOMEM;
1884 if (!((data->info->func[page] & PMBUS_HAVE_PWM12) ||
1885 (data->info->func[page] & PMBUS_HAVE_PWM34)))
1886 return 0;
1888 sensor = pmbus_add_sensor(data, "pwm", NULL, index, page,
1889 0xff, PMBUS_VIRT_PWM_1 + id, PSC_PWM,
1890 false, false, true);
1892 if (!sensor)
1893 return -ENOMEM;
1895 sensor = pmbus_add_sensor(data, "pwm", "enable", index, page,
1896 0xff, PMBUS_VIRT_PWM_ENABLE_1 + id, PSC_PWM,
1897 true, false, false);
1899 if (!sensor)
1900 return -ENOMEM;
1902 return 0;
1905 static int pmbus_add_fan_attributes(struct i2c_client *client,
1906 struct pmbus_data *data)
1908 const struct pmbus_driver_info *info = data->info;
1909 int index = 1;
1910 int page;
1911 int ret;
1913 for (page = 0; page < info->pages; page++) {
1914 int f;
1916 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1917 int regval;
1919 if (!(info->func[page] & pmbus_fan_flags[f]))
1920 break;
1922 if (!pmbus_check_word_register(client, page,
1923 pmbus_fan_registers[f]))
1924 break;
1927 * Skip fan if not installed.
1928 * Each fan configuration register covers multiple fans,
1929 * so we have to do some magic.
1931 regval = _pmbus_read_byte_data(client, page,
1932 pmbus_fan_config_registers[f]);
1933 if (regval < 0 ||
1934 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1935 continue;
1937 if (pmbus_add_sensor(data, "fan", "input", index,
1938 page, 0xff, pmbus_fan_registers[f],
1939 PSC_FAN, true, true, true) == NULL)
1940 return -ENOMEM;
1942 /* Fan control */
1943 if (pmbus_check_word_register(client, page,
1944 pmbus_fan_command_registers[f])) {
1945 ret = pmbus_add_fan_ctrl(client, data, index,
1946 page, f, regval);
1947 if (ret < 0)
1948 return ret;
1952 * Each fan status register covers multiple fans,
1953 * so we have to do some magic.
1955 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1956 pmbus_check_byte_register(client,
1957 page, pmbus_fan_status_registers[f])) {
1958 int reg;
1960 if (f > 1) /* fan 3, 4 */
1961 reg = PMBUS_STATUS_FAN_34;
1962 else
1963 reg = PMBUS_STATUS_FAN_12;
1964 ret = pmbus_add_boolean(data, "fan",
1965 "alarm", index, NULL, NULL, page, reg,
1966 PB_FAN_FAN1_WARNING >> (f & 1));
1967 if (ret)
1968 return ret;
1969 ret = pmbus_add_boolean(data, "fan",
1970 "fault", index, NULL, NULL, page, reg,
1971 PB_FAN_FAN1_FAULT >> (f & 1));
1972 if (ret)
1973 return ret;
1975 index++;
1978 return 0;
1981 struct pmbus_samples_attr {
1982 int reg;
1983 char *name;
1986 struct pmbus_samples_reg {
1987 int page;
1988 struct pmbus_samples_attr *attr;
1989 struct device_attribute dev_attr;
1992 static struct pmbus_samples_attr pmbus_samples_registers[] = {
1994 .reg = PMBUS_VIRT_SAMPLES,
1995 .name = "samples",
1996 }, {
1997 .reg = PMBUS_VIRT_IN_SAMPLES,
1998 .name = "in_samples",
1999 }, {
2000 .reg = PMBUS_VIRT_CURR_SAMPLES,
2001 .name = "curr_samples",
2002 }, {
2003 .reg = PMBUS_VIRT_POWER_SAMPLES,
2004 .name = "power_samples",
2005 }, {
2006 .reg = PMBUS_VIRT_TEMP_SAMPLES,
2007 .name = "temp_samples",
2011 #define to_samples_reg(x) container_of(x, struct pmbus_samples_reg, dev_attr)
2013 static ssize_t pmbus_show_samples(struct device *dev,
2014 struct device_attribute *devattr, char *buf)
2016 int val;
2017 struct i2c_client *client = to_i2c_client(dev->parent);
2018 struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2019 struct pmbus_data *data = i2c_get_clientdata(client);
2021 mutex_lock(&data->update_lock);
2022 val = _pmbus_read_word_data(client, reg->page, 0xff, reg->attr->reg);
2023 mutex_unlock(&data->update_lock);
2024 if (val < 0)
2025 return val;
2027 return snprintf(buf, PAGE_SIZE, "%d\n", val);
2030 static ssize_t pmbus_set_samples(struct device *dev,
2031 struct device_attribute *devattr,
2032 const char *buf, size_t count)
2034 int ret;
2035 long val;
2036 struct i2c_client *client = to_i2c_client(dev->parent);
2037 struct pmbus_samples_reg *reg = to_samples_reg(devattr);
2038 struct pmbus_data *data = i2c_get_clientdata(client);
2040 if (kstrtol(buf, 0, &val) < 0)
2041 return -EINVAL;
2043 mutex_lock(&data->update_lock);
2044 ret = _pmbus_write_word_data(client, reg->page, reg->attr->reg, val);
2045 mutex_unlock(&data->update_lock);
2047 return ret ? : count;
2050 static int pmbus_add_samples_attr(struct pmbus_data *data, int page,
2051 struct pmbus_samples_attr *attr)
2053 struct pmbus_samples_reg *reg;
2055 reg = devm_kzalloc(data->dev, sizeof(*reg), GFP_KERNEL);
2056 if (!reg)
2057 return -ENOMEM;
2059 reg->attr = attr;
2060 reg->page = page;
2062 pmbus_dev_attr_init(&reg->dev_attr, attr->name, 0644,
2063 pmbus_show_samples, pmbus_set_samples);
2065 return pmbus_add_attribute(data, &reg->dev_attr.attr);
2068 static int pmbus_add_samples_attributes(struct i2c_client *client,
2069 struct pmbus_data *data)
2071 const struct pmbus_driver_info *info = data->info;
2072 int s;
2074 if (!(info->func[0] & PMBUS_HAVE_SAMPLES))
2075 return 0;
2077 for (s = 0; s < ARRAY_SIZE(pmbus_samples_registers); s++) {
2078 struct pmbus_samples_attr *attr;
2079 int ret;
2081 attr = &pmbus_samples_registers[s];
2082 if (!pmbus_check_word_register(client, 0, attr->reg))
2083 continue;
2085 ret = pmbus_add_samples_attr(data, 0, attr);
2086 if (ret)
2087 return ret;
2090 return 0;
2093 static int pmbus_find_attributes(struct i2c_client *client,
2094 struct pmbus_data *data)
2096 int ret;
2098 /* Voltage sensors */
2099 ret = pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
2100 ARRAY_SIZE(voltage_attributes));
2101 if (ret)
2102 return ret;
2104 /* Current sensors */
2105 ret = pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
2106 ARRAY_SIZE(current_attributes));
2107 if (ret)
2108 return ret;
2110 /* Power sensors */
2111 ret = pmbus_add_sensor_attrs(client, data, "power", power_attributes,
2112 ARRAY_SIZE(power_attributes));
2113 if (ret)
2114 return ret;
2116 /* Temperature sensors */
2117 ret = pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
2118 ARRAY_SIZE(temp_attributes));
2119 if (ret)
2120 return ret;
2122 /* Fans */
2123 ret = pmbus_add_fan_attributes(client, data);
2124 if (ret)
2125 return ret;
2127 ret = pmbus_add_samples_attributes(client, data);
2128 return ret;
2132 * Identify chip parameters.
2133 * This function is called for all chips.
2135 static int pmbus_identify_common(struct i2c_client *client,
2136 struct pmbus_data *data, int page)
2138 int vout_mode = -1;
2140 if (pmbus_check_byte_register(client, page, PMBUS_VOUT_MODE))
2141 vout_mode = _pmbus_read_byte_data(client, page,
2142 PMBUS_VOUT_MODE);
2143 if (vout_mode >= 0 && vout_mode != 0xff) {
2145 * Not all chips support the VOUT_MODE command,
2146 * so a failure to read it is not an error.
2148 switch (vout_mode >> 5) {
2149 case 0: /* linear mode */
2150 if (data->info->format[PSC_VOLTAGE_OUT] != linear)
2151 return -ENODEV;
2153 data->exponent[page] = ((s8)(vout_mode << 3)) >> 3;
2154 break;
2155 case 1: /* VID mode */
2156 if (data->info->format[PSC_VOLTAGE_OUT] != vid)
2157 return -ENODEV;
2158 break;
2159 case 2: /* direct mode */
2160 if (data->info->format[PSC_VOLTAGE_OUT] != direct)
2161 return -ENODEV;
2162 break;
2163 default:
2164 return -ENODEV;
2168 pmbus_clear_fault_page(client, page);
2169 return 0;
2172 static int pmbus_read_status_byte(struct i2c_client *client, int page)
2174 return _pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
2177 static int pmbus_read_status_word(struct i2c_client *client, int page)
2179 return _pmbus_read_word_data(client, page, 0xff, PMBUS_STATUS_WORD);
2182 static int pmbus_init_common(struct i2c_client *client, struct pmbus_data *data,
2183 struct pmbus_driver_info *info)
2185 struct device *dev = &client->dev;
2186 int page, ret;
2189 * Some PMBus chips don't support PMBUS_STATUS_WORD, so try
2190 * to use PMBUS_STATUS_BYTE instead if that is the case.
2191 * Bail out if both registers are not supported.
2193 data->read_status = pmbus_read_status_word;
2194 ret = i2c_smbus_read_word_data(client, PMBUS_STATUS_WORD);
2195 if (ret < 0 || ret == 0xffff) {
2196 data->read_status = pmbus_read_status_byte;
2197 ret = i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE);
2198 if (ret < 0 || ret == 0xff) {
2199 dev_err(dev, "PMBus status register not found\n");
2200 return -ENODEV;
2202 } else {
2203 data->has_status_word = true;
2206 /* Enable PEC if the controller supports it */
2207 ret = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2208 if (ret >= 0 && (ret & PB_CAPABILITY_ERROR_CHECK))
2209 client->flags |= I2C_CLIENT_PEC;
2212 * Check if the chip is write protected. If it is, we can not clear
2213 * faults, and we should not try it. Also, in that case, writes into
2214 * limit registers need to be disabled.
2216 ret = i2c_smbus_read_byte_data(client, PMBUS_WRITE_PROTECT);
2217 if (ret > 0 && (ret & PB_WP_ANY))
2218 data->flags |= PMBUS_WRITE_PROTECTED | PMBUS_SKIP_STATUS_CHECK;
2220 if (data->info->pages)
2221 pmbus_clear_faults(client);
2222 else
2223 pmbus_clear_fault_page(client, -1);
2225 if (info->identify) {
2226 ret = (*info->identify)(client, info);
2227 if (ret < 0) {
2228 dev_err(dev, "Chip identification failed\n");
2229 return ret;
2233 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
2234 dev_err(dev, "Bad number of PMBus pages: %d\n", info->pages);
2235 return -ENODEV;
2238 for (page = 0; page < info->pages; page++) {
2239 ret = pmbus_identify_common(client, data, page);
2240 if (ret < 0) {
2241 dev_err(dev, "Failed to identify chip capabilities\n");
2242 return ret;
2245 return 0;
2248 #if IS_ENABLED(CONFIG_REGULATOR)
2249 static int pmbus_regulator_is_enabled(struct regulator_dev *rdev)
2251 struct device *dev = rdev_get_dev(rdev);
2252 struct i2c_client *client = to_i2c_client(dev->parent);
2253 u8 page = rdev_get_id(rdev);
2254 int ret;
2256 ret = pmbus_read_byte_data(client, page, PMBUS_OPERATION);
2257 if (ret < 0)
2258 return ret;
2260 return !!(ret & PB_OPERATION_CONTROL_ON);
2263 static int _pmbus_regulator_on_off(struct regulator_dev *rdev, bool enable)
2265 struct device *dev = rdev_get_dev(rdev);
2266 struct i2c_client *client = to_i2c_client(dev->parent);
2267 u8 page = rdev_get_id(rdev);
2269 return pmbus_update_byte_data(client, page, PMBUS_OPERATION,
2270 PB_OPERATION_CONTROL_ON,
2271 enable ? PB_OPERATION_CONTROL_ON : 0);
2274 static int pmbus_regulator_enable(struct regulator_dev *rdev)
2276 return _pmbus_regulator_on_off(rdev, 1);
2279 static int pmbus_regulator_disable(struct regulator_dev *rdev)
2281 return _pmbus_regulator_on_off(rdev, 0);
2284 const struct regulator_ops pmbus_regulator_ops = {
2285 .enable = pmbus_regulator_enable,
2286 .disable = pmbus_regulator_disable,
2287 .is_enabled = pmbus_regulator_is_enabled,
2289 EXPORT_SYMBOL_GPL(pmbus_regulator_ops);
2291 static int pmbus_regulator_register(struct pmbus_data *data)
2293 struct device *dev = data->dev;
2294 const struct pmbus_driver_info *info = data->info;
2295 const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2296 struct regulator_dev *rdev;
2297 int i;
2299 for (i = 0; i < info->num_regulators; i++) {
2300 struct regulator_config config = { };
2302 config.dev = dev;
2303 config.driver_data = data;
2305 if (pdata && pdata->reg_init_data)
2306 config.init_data = &pdata->reg_init_data[i];
2308 rdev = devm_regulator_register(dev, &info->reg_desc[i],
2309 &config);
2310 if (IS_ERR(rdev)) {
2311 dev_err(dev, "Failed to register %s regulator\n",
2312 info->reg_desc[i].name);
2313 return PTR_ERR(rdev);
2317 return 0;
2319 #else
2320 static int pmbus_regulator_register(struct pmbus_data *data)
2322 return 0;
2324 #endif
2326 static struct dentry *pmbus_debugfs_dir; /* pmbus debugfs directory */
2328 #if IS_ENABLED(CONFIG_DEBUG_FS)
2329 static int pmbus_debugfs_get(void *data, u64 *val)
2331 int rc;
2332 struct pmbus_debugfs_entry *entry = data;
2334 rc = _pmbus_read_byte_data(entry->client, entry->page, entry->reg);
2335 if (rc < 0)
2336 return rc;
2338 *val = rc;
2340 return 0;
2342 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops, pmbus_debugfs_get, NULL,
2343 "0x%02llx\n");
2345 static int pmbus_debugfs_get_status(void *data, u64 *val)
2347 int rc;
2348 struct pmbus_debugfs_entry *entry = data;
2349 struct pmbus_data *pdata = i2c_get_clientdata(entry->client);
2351 rc = pdata->read_status(entry->client, entry->page);
2352 if (rc < 0)
2353 return rc;
2355 *val = rc;
2357 return 0;
2359 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_status, pmbus_debugfs_get_status,
2360 NULL, "0x%04llx\n");
2362 static int pmbus_debugfs_get_pec(void *data, u64 *val)
2364 struct i2c_client *client = data;
2366 *val = !!(client->flags & I2C_CLIENT_PEC);
2368 return 0;
2371 static int pmbus_debugfs_set_pec(void *data, u64 val)
2373 int rc;
2374 struct i2c_client *client = data;
2376 if (!val) {
2377 client->flags &= ~I2C_CLIENT_PEC;
2378 return 0;
2381 if (val != 1)
2382 return -EINVAL;
2384 rc = i2c_smbus_read_byte_data(client, PMBUS_CAPABILITY);
2385 if (rc < 0)
2386 return rc;
2388 if (!(rc & PB_CAPABILITY_ERROR_CHECK))
2389 return -EOPNOTSUPP;
2391 client->flags |= I2C_CLIENT_PEC;
2393 return 0;
2395 DEFINE_DEBUGFS_ATTRIBUTE(pmbus_debugfs_ops_pec, pmbus_debugfs_get_pec,
2396 pmbus_debugfs_set_pec, "%llu\n");
2398 static void pmbus_remove_debugfs(void *data)
2400 struct dentry *entry = data;
2402 debugfs_remove_recursive(entry);
2405 static int pmbus_init_debugfs(struct i2c_client *client,
2406 struct pmbus_data *data)
2408 int i, idx = 0;
2409 char name[PMBUS_NAME_SIZE];
2410 struct pmbus_debugfs_entry *entries;
2412 if (!pmbus_debugfs_dir)
2413 return -ENODEV;
2416 * Create the debugfs directory for this device. Use the hwmon device
2417 * name to avoid conflicts (hwmon numbers are globally unique).
2419 data->debugfs = debugfs_create_dir(dev_name(data->hwmon_dev),
2420 pmbus_debugfs_dir);
2421 if (IS_ERR_OR_NULL(data->debugfs)) {
2422 data->debugfs = NULL;
2423 return -ENODEV;
2426 /* Allocate the max possible entries we need. */
2427 entries = devm_kcalloc(data->dev,
2428 data->info->pages * 10, sizeof(*entries),
2429 GFP_KERNEL);
2430 if (!entries)
2431 return -ENOMEM;
2433 debugfs_create_file("pec", 0664, data->debugfs, client,
2434 &pmbus_debugfs_ops_pec);
2436 for (i = 0; i < data->info->pages; ++i) {
2437 /* Check accessibility of status register if it's not page 0 */
2438 if (!i || pmbus_check_status_register(client, i)) {
2439 /* No need to set reg as we have special read op. */
2440 entries[idx].client = client;
2441 entries[idx].page = i;
2442 scnprintf(name, PMBUS_NAME_SIZE, "status%d", i);
2443 debugfs_create_file(name, 0444, data->debugfs,
2444 &entries[idx++],
2445 &pmbus_debugfs_ops_status);
2448 if (data->info->func[i] & PMBUS_HAVE_STATUS_VOUT) {
2449 entries[idx].client = client;
2450 entries[idx].page = i;
2451 entries[idx].reg = PMBUS_STATUS_VOUT;
2452 scnprintf(name, PMBUS_NAME_SIZE, "status%d_vout", i);
2453 debugfs_create_file(name, 0444, data->debugfs,
2454 &entries[idx++],
2455 &pmbus_debugfs_ops);
2458 if (data->info->func[i] & PMBUS_HAVE_STATUS_IOUT) {
2459 entries[idx].client = client;
2460 entries[idx].page = i;
2461 entries[idx].reg = PMBUS_STATUS_IOUT;
2462 scnprintf(name, PMBUS_NAME_SIZE, "status%d_iout", i);
2463 debugfs_create_file(name, 0444, data->debugfs,
2464 &entries[idx++],
2465 &pmbus_debugfs_ops);
2468 if (data->info->func[i] & PMBUS_HAVE_STATUS_INPUT) {
2469 entries[idx].client = client;
2470 entries[idx].page = i;
2471 entries[idx].reg = PMBUS_STATUS_INPUT;
2472 scnprintf(name, PMBUS_NAME_SIZE, "status%d_input", i);
2473 debugfs_create_file(name, 0444, data->debugfs,
2474 &entries[idx++],
2475 &pmbus_debugfs_ops);
2478 if (data->info->func[i] & PMBUS_HAVE_STATUS_TEMP) {
2479 entries[idx].client = client;
2480 entries[idx].page = i;
2481 entries[idx].reg = PMBUS_STATUS_TEMPERATURE;
2482 scnprintf(name, PMBUS_NAME_SIZE, "status%d_temp", i);
2483 debugfs_create_file(name, 0444, data->debugfs,
2484 &entries[idx++],
2485 &pmbus_debugfs_ops);
2488 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_CML)) {
2489 entries[idx].client = client;
2490 entries[idx].page = i;
2491 entries[idx].reg = PMBUS_STATUS_CML;
2492 scnprintf(name, PMBUS_NAME_SIZE, "status%d_cml", i);
2493 debugfs_create_file(name, 0444, data->debugfs,
2494 &entries[idx++],
2495 &pmbus_debugfs_ops);
2498 if (pmbus_check_byte_register(client, i, PMBUS_STATUS_OTHER)) {
2499 entries[idx].client = client;
2500 entries[idx].page = i;
2501 entries[idx].reg = PMBUS_STATUS_OTHER;
2502 scnprintf(name, PMBUS_NAME_SIZE, "status%d_other", i);
2503 debugfs_create_file(name, 0444, data->debugfs,
2504 &entries[idx++],
2505 &pmbus_debugfs_ops);
2508 if (pmbus_check_byte_register(client, i,
2509 PMBUS_STATUS_MFR_SPECIFIC)) {
2510 entries[idx].client = client;
2511 entries[idx].page = i;
2512 entries[idx].reg = PMBUS_STATUS_MFR_SPECIFIC;
2513 scnprintf(name, PMBUS_NAME_SIZE, "status%d_mfr", i);
2514 debugfs_create_file(name, 0444, data->debugfs,
2515 &entries[idx++],
2516 &pmbus_debugfs_ops);
2519 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN12) {
2520 entries[idx].client = client;
2521 entries[idx].page = i;
2522 entries[idx].reg = PMBUS_STATUS_FAN_12;
2523 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan12", i);
2524 debugfs_create_file(name, 0444, data->debugfs,
2525 &entries[idx++],
2526 &pmbus_debugfs_ops);
2529 if (data->info->func[i] & PMBUS_HAVE_STATUS_FAN34) {
2530 entries[idx].client = client;
2531 entries[idx].page = i;
2532 entries[idx].reg = PMBUS_STATUS_FAN_34;
2533 scnprintf(name, PMBUS_NAME_SIZE, "status%d_fan34", i);
2534 debugfs_create_file(name, 0444, data->debugfs,
2535 &entries[idx++],
2536 &pmbus_debugfs_ops);
2540 return devm_add_action_or_reset(data->dev,
2541 pmbus_remove_debugfs, data->debugfs);
2543 #else
2544 static int pmbus_init_debugfs(struct i2c_client *client,
2545 struct pmbus_data *data)
2547 return 0;
2549 #endif /* IS_ENABLED(CONFIG_DEBUG_FS) */
2551 int pmbus_do_probe(struct i2c_client *client, struct pmbus_driver_info *info)
2553 struct device *dev = &client->dev;
2554 const struct pmbus_platform_data *pdata = dev_get_platdata(dev);
2555 struct pmbus_data *data;
2556 size_t groups_num = 0;
2557 int ret;
2559 if (!info)
2560 return -ENODEV;
2562 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
2563 | I2C_FUNC_SMBUS_BYTE_DATA
2564 | I2C_FUNC_SMBUS_WORD_DATA))
2565 return -ENODEV;
2567 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
2568 if (!data)
2569 return -ENOMEM;
2571 if (info->groups)
2572 while (info->groups[groups_num])
2573 groups_num++;
2575 data->groups = devm_kcalloc(dev, groups_num + 2, sizeof(void *),
2576 GFP_KERNEL);
2577 if (!data->groups)
2578 return -ENOMEM;
2580 i2c_set_clientdata(client, data);
2581 mutex_init(&data->update_lock);
2582 data->dev = dev;
2584 if (pdata)
2585 data->flags = pdata->flags;
2586 data->info = info;
2587 data->currpage = -1;
2588 data->currphase = -1;
2590 ret = pmbus_init_common(client, data, info);
2591 if (ret < 0)
2592 return ret;
2594 ret = pmbus_find_attributes(client, data);
2595 if (ret)
2596 return ret;
2599 * If there are no attributes, something is wrong.
2600 * Bail out instead of trying to register nothing.
2602 if (!data->num_attributes) {
2603 dev_err(dev, "No attributes found\n");
2604 return -ENODEV;
2607 data->groups[0] = &data->group;
2608 memcpy(data->groups + 1, info->groups, sizeof(void *) * groups_num);
2609 data->hwmon_dev = devm_hwmon_device_register_with_groups(dev,
2610 client->name, data, data->groups);
2611 if (IS_ERR(data->hwmon_dev)) {
2612 dev_err(dev, "Failed to register hwmon device\n");
2613 return PTR_ERR(data->hwmon_dev);
2616 ret = pmbus_regulator_register(data);
2617 if (ret)
2618 return ret;
2620 ret = pmbus_init_debugfs(client, data);
2621 if (ret)
2622 dev_warn(dev, "Failed to register debugfs\n");
2624 return 0;
2626 EXPORT_SYMBOL_GPL(pmbus_do_probe);
2628 struct dentry *pmbus_get_debugfs_dir(struct i2c_client *client)
2630 struct pmbus_data *data = i2c_get_clientdata(client);
2632 return data->debugfs;
2634 EXPORT_SYMBOL_GPL(pmbus_get_debugfs_dir);
2636 static int __init pmbus_core_init(void)
2638 pmbus_debugfs_dir = debugfs_create_dir("pmbus", NULL);
2639 if (IS_ERR(pmbus_debugfs_dir))
2640 pmbus_debugfs_dir = NULL;
2642 return 0;
2645 static void __exit pmbus_core_exit(void)
2647 debugfs_remove_recursive(pmbus_debugfs_dir);
2650 module_init(pmbus_core_init);
2651 module_exit(pmbus_core_exit);
2653 MODULE_AUTHOR("Guenter Roeck");
2654 MODULE_DESCRIPTION("PMBus core driver");
2655 MODULE_LICENSE("GPL");