2 * Hardware monitoring driver for PMBus devices
4 * Copyright (c) 2010, 2011 Ericsson AB.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/delay.h>
30 #include <linux/i2c/pmbus.h>
34 * Constants needed to determine number of sensors, booleans, and labels.
36 #define PMBUS_MAX_INPUT_SENSORS 11 /* 6*volt, 3*curr, 2*power */
37 #define PMBUS_VOUT_SENSORS_PER_PAGE 5 /* input, min, max, lcrit,
39 #define PMBUS_IOUT_SENSORS_PER_PAGE 4 /* input, min, max, crit */
40 #define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */
41 #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
42 #define PMBUS_MAX_SENSORS_PER_TEMP 5 /* input, min, max, lcrit,
45 #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
46 lcrit_alarm, crit_alarm;
49 #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
50 lcrit_alarm, crit_alarm */
51 #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
53 #define PMBUS_POUT_BOOLEANS_PER_PAGE 2 /* alarm, crit_alarm */
54 #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
55 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
56 lcrit_alarm, crit_alarm */
58 #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
61 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
62 * are paged. status_input is unpaged.
64 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
67 * Index into status register array, per status register group
69 #define PB_STATUS_BASE 0
70 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
71 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
72 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
73 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
74 #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
75 #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
78 char name[I2C_NAME_SIZE]; /* sysfs sensor name */
79 struct sensor_device_attribute attribute;
80 u8 page; /* page number */
81 u8 reg; /* register */
82 enum pmbus_sensor_classes class; /* sensor class */
83 bool update; /* runtime sensor update needed */
84 int data; /* Sensor data.
85 Negative if there was a read error */
88 struct pmbus_boolean {
89 char name[I2C_NAME_SIZE]; /* sysfs boolean name */
90 struct sensor_device_attribute attribute;
94 char name[I2C_NAME_SIZE]; /* sysfs label name */
95 struct sensor_device_attribute attribute;
96 char label[I2C_NAME_SIZE]; /* label */
100 struct device *hwmon_dev;
102 u32 flags; /* from platform data */
104 int exponent; /* linear mode: exponent for output voltages */
106 const struct pmbus_driver_info *info;
110 struct attribute **attributes;
111 struct attribute_group group;
114 * Sensors cover both sensor and limit registers.
118 struct pmbus_sensor *sensors;
120 * Booleans are used for alarms.
121 * Values are determined from status registers.
125 struct pmbus_boolean *booleans;
127 * Labels are used to map generic names (e.g., "in1")
128 * to PMBus specific names (e.g., "vin" or "vout1").
132 struct pmbus_label *labels;
134 struct mutex update_lock;
136 unsigned long last_updated; /* in jiffies */
139 * A single status register covers multiple attributes,
140 * so we keep them all together.
142 u8 status[PB_NUM_STATUS_REG];
147 int pmbus_set_page(struct i2c_client *client, u8 page)
149 struct pmbus_data *data = i2c_get_clientdata(client);
153 if (page != data->currpage) {
154 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
155 newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
159 data->currpage = page;
163 EXPORT_SYMBOL_GPL(pmbus_set_page);
165 static int pmbus_write_byte(struct i2c_client *client, u8 page, u8 value)
169 rv = pmbus_set_page(client, page);
173 return i2c_smbus_write_byte(client, value);
176 static int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg,
181 rv = pmbus_set_page(client, page);
185 return i2c_smbus_write_word_data(client, reg, word);
188 int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
192 rv = pmbus_set_page(client, page);
196 return i2c_smbus_read_word_data(client, reg);
198 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
200 static int pmbus_read_byte_data(struct i2c_client *client, u8 page, u8 reg)
204 rv = pmbus_set_page(client, page);
208 return i2c_smbus_read_byte_data(client, reg);
211 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
213 pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
216 void pmbus_clear_faults(struct i2c_client *client)
218 struct pmbus_data *data = i2c_get_clientdata(client);
221 for (i = 0; i < data->info->pages; i++)
222 pmbus_clear_fault_page(client, i);
224 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
226 static int pmbus_check_status_cml(struct i2c_client *client, int page)
230 status = pmbus_read_byte_data(client, page, PMBUS_STATUS_BYTE);
231 if (status < 0 || (status & PB_STATUS_CML)) {
232 status2 = pmbus_read_byte_data(client, page, PMBUS_STATUS_CML);
233 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
239 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
242 struct pmbus_data *data = i2c_get_clientdata(client);
244 rv = pmbus_read_byte_data(client, page, reg);
245 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
246 rv = pmbus_check_status_cml(client, page);
247 pmbus_clear_fault_page(client, page);
250 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
252 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
255 struct pmbus_data *data = i2c_get_clientdata(client);
257 rv = pmbus_read_word_data(client, page, reg);
258 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
259 rv = pmbus_check_status_cml(client, page);
260 pmbus_clear_fault_page(client, page);
263 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
265 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
267 struct pmbus_data *data = i2c_get_clientdata(client);
271 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
274 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
275 * a device specific mapping funcion exists and calls it if necessary.
277 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
279 struct pmbus_data *data = i2c_get_clientdata(client);
280 const struct pmbus_driver_info *info = data->info;
283 if (info->read_byte_data) {
284 status = info->read_byte_data(client, page, reg);
285 if (status != -ENODATA)
288 return pmbus_read_byte_data(client, page, reg);
291 static struct pmbus_data *pmbus_update_device(struct device *dev)
293 struct i2c_client *client = to_i2c_client(dev);
294 struct pmbus_data *data = i2c_get_clientdata(client);
295 const struct pmbus_driver_info *info = data->info;
297 mutex_lock(&data->update_lock);
298 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
301 for (i = 0; i < info->pages; i++)
302 data->status[PB_STATUS_BASE + i]
303 = pmbus_read_byte_data(client, i,
305 for (i = 0; i < info->pages; i++) {
306 if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT))
308 data->status[PB_STATUS_VOUT_BASE + i]
309 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_VOUT);
311 for (i = 0; i < info->pages; i++) {
312 if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT))
314 data->status[PB_STATUS_IOUT_BASE + i]
315 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_IOUT);
317 for (i = 0; i < info->pages; i++) {
318 if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP))
320 data->status[PB_STATUS_TEMP_BASE + i]
321 = _pmbus_read_byte_data(client, i,
322 PMBUS_STATUS_TEMPERATURE);
324 for (i = 0; i < info->pages; i++) {
325 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12))
327 data->status[PB_STATUS_FAN_BASE + i]
328 = _pmbus_read_byte_data(client, i,
329 PMBUS_STATUS_FAN_12);
332 for (i = 0; i < info->pages; i++) {
333 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34))
335 data->status[PB_STATUS_FAN34_BASE + i]
336 = _pmbus_read_byte_data(client, i,
337 PMBUS_STATUS_FAN_34);
340 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
341 data->status[PB_STATUS_INPUT_BASE]
342 = _pmbus_read_byte_data(client, 0,
345 for (i = 0; i < data->num_sensors; i++) {
346 struct pmbus_sensor *sensor = &data->sensors[i];
348 if (!data->valid || sensor->update)
350 = pmbus_read_word_data(client, sensor->page,
353 pmbus_clear_faults(client);
354 data->last_updated = jiffies;
357 mutex_unlock(&data->update_lock);
362 * Convert linear sensor values to milli- or micro-units
363 * depending on sensor type.
365 static int pmbus_reg2data_linear(struct pmbus_data *data,
366 struct pmbus_sensor *sensor)
372 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
373 exponent = data->exponent;
374 mantissa = (u16) sensor->data;
375 } else { /* LINEAR11 */
376 exponent = (sensor->data >> 11) & 0x001f;
377 mantissa = sensor->data & 0x07ff;
380 exponent |= 0xffe0; /* sign extend exponent */
381 if (mantissa > 0x03ff)
382 mantissa |= 0xfffff800; /* sign extend mantissa */
387 /* scale result to milli-units for all sensors except fans */
388 if (sensor->class != PSC_FAN)
391 /* scale result to micro-units for power sensors */
392 if (sensor->class == PSC_POWER)
404 * Convert direct sensor values to milli- or micro-units
405 * depending on sensor type.
407 static int pmbus_reg2data_direct(struct pmbus_data *data,
408 struct pmbus_sensor *sensor)
410 long val = (s16) sensor->data;
413 m = data->info->m[sensor->class];
414 b = data->info->b[sensor->class];
415 R = data->info->R[sensor->class];
420 /* X = 1/m * (Y * 10^-R - b) */
422 /* scale result to milli-units for everything but fans */
423 if (sensor->class != PSC_FAN) {
428 /* scale result to micro-units for power sensors */
429 if (sensor->class == PSC_POWER) {
439 val = DIV_ROUND_CLOSEST(val, 10);
443 return (int)((val - b) / m);
446 static int pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
450 if (data->info->direct[sensor->class])
451 val = pmbus_reg2data_direct(data, sensor);
453 val = pmbus_reg2data_linear(data, sensor);
458 #define MAX_MANTISSA (1023 * 1000)
459 #define MIN_MANTISSA (511 * 1000)
461 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
462 enum pmbus_sensor_classes class, long val)
464 s16 exponent = 0, mantissa;
465 bool negative = false;
471 if (class == PSC_VOLTAGE_OUT) {
472 /* LINEAR16 does not support negative voltages */
477 * For a static exponents, we don't have a choice
478 * but to adjust the value to it.
480 if (data->exponent < 0)
481 val <<= -data->exponent;
483 val >>= data->exponent;
484 val = DIV_ROUND_CLOSEST(val, 1000);
493 /* Power is in uW. Convert to mW before converting. */
494 if (class == PSC_POWER)
495 val = DIV_ROUND_CLOSEST(val, 1000L);
498 * For simplicity, convert fan data to milli-units
499 * before calculating the exponent.
501 if (class == PSC_FAN)
504 /* Reduce large mantissa until it fits into 10 bit */
505 while (val >= MAX_MANTISSA && exponent < 15) {
509 /* Increase small mantissa to improve precision */
510 while (val < MIN_MANTISSA && exponent > -15) {
515 /* Convert mantissa from milli-units to units */
516 mantissa = DIV_ROUND_CLOSEST(val, 1000);
518 /* Ensure that resulting number is within range */
519 if (mantissa > 0x3ff)
524 mantissa = -mantissa;
526 /* Convert to 5 bit exponent, 11 bit mantissa */
527 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
530 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
531 enum pmbus_sensor_classes class, long val)
535 m = data->info->m[class];
536 b = data->info->b[class];
537 R = data->info->R[class];
539 /* Power is in uW. Adjust R and b. */
540 if (class == PSC_POWER) {
545 /* Calculate Y = (m * X + b) * 10^R */
546 if (class != PSC_FAN) {
547 R -= 3; /* Adjust R and b for data in milli-units */
557 val = DIV_ROUND_CLOSEST(val, 10);
564 static u16 pmbus_data2reg(struct pmbus_data *data,
565 enum pmbus_sensor_classes class, long val)
569 if (data->info->direct[class])
570 regval = pmbus_data2reg_direct(data, class, val);
572 regval = pmbus_data2reg_linear(data, class, val);
578 * Return boolean calculated from converted data.
579 * <index> defines a status register index and mask, and optionally
580 * two sensor indexes.
581 * The upper half-word references the two sensors,
582 * two sensor indices.
583 * The upper half-word references the two optional sensors,
584 * the lower half word references status register and mask.
585 * The function returns true if (status[reg] & mask) is true and,
586 * if specified, if v1 >= v2.
587 * To determine if an object exceeds upper limits, specify <v, limit>.
588 * To determine if an object exceeds lower limits, specify <limit, v>.
590 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
591 * index are set. s1 and s2 (the sensor index values) are zero in this case.
592 * The function returns true if (status[reg] & mask) is true.
594 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
595 * a specified limit has to be performed to determine the boolean result.
596 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
597 * sensor values referenced by sensor indices s1 and s2).
599 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
600 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
602 * If a negative value is stored in any of the referenced registers, this value
603 * reflects an error code which will be returned.
605 static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val)
607 u8 s1 = (index >> 24) & 0xff;
608 u8 s2 = (index >> 16) & 0xff;
609 u8 reg = (index >> 8) & 0xff;
610 u8 mask = index & 0xff;
614 status = data->status[reg];
618 regval = status & mask;
623 struct pmbus_sensor *sensor1, *sensor2;
625 sensor1 = &data->sensors[s1];
626 if (sensor1->data < 0)
627 return sensor1->data;
628 sensor2 = &data->sensors[s2];
629 if (sensor2->data < 0)
630 return sensor2->data;
632 v1 = pmbus_reg2data(data, sensor1);
633 v2 = pmbus_reg2data(data, sensor2);
634 *val = !!(regval && v1 >= v2);
639 static ssize_t pmbus_show_boolean(struct device *dev,
640 struct device_attribute *da, char *buf)
642 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
643 struct pmbus_data *data = pmbus_update_device(dev);
647 err = pmbus_get_boolean(data, attr->index, &val);
650 return snprintf(buf, PAGE_SIZE, "%d\n", val);
653 static ssize_t pmbus_show_sensor(struct device *dev,
654 struct device_attribute *da, char *buf)
656 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
657 struct pmbus_data *data = pmbus_update_device(dev);
658 struct pmbus_sensor *sensor;
660 sensor = &data->sensors[attr->index];
661 if (sensor->data < 0)
664 return snprintf(buf, PAGE_SIZE, "%d\n", pmbus_reg2data(data, sensor));
667 static ssize_t pmbus_set_sensor(struct device *dev,
668 struct device_attribute *devattr,
669 const char *buf, size_t count)
671 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
672 struct i2c_client *client = to_i2c_client(dev);
673 struct pmbus_data *data = i2c_get_clientdata(client);
674 struct pmbus_sensor *sensor = &data->sensors[attr->index];
680 if (strict_strtol(buf, 10, &val) < 0)
683 mutex_lock(&data->update_lock);
684 regval = pmbus_data2reg(data, sensor->class, val);
685 ret = pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
689 data->sensors[attr->index].data = regval;
690 mutex_unlock(&data->update_lock);
694 static ssize_t pmbus_show_label(struct device *dev,
695 struct device_attribute *da, char *buf)
697 struct i2c_client *client = to_i2c_client(dev);
698 struct pmbus_data *data = i2c_get_clientdata(client);
699 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
701 return snprintf(buf, PAGE_SIZE, "%s\n",
702 data->labels[attr->index].label);
705 #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
707 struct sensor_device_attribute *a \
708 = &data->_type##s[data->num_##_type##s].attribute; \
709 BUG_ON(data->num_attributes >= data->max_attributes); \
710 a->dev_attr.attr.name = _name; \
711 a->dev_attr.attr.mode = _mode; \
712 a->dev_attr.show = _show; \
713 a->dev_attr.store = _set; \
715 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
716 data->num_attributes++; \
719 #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
720 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
721 pmbus_show_##_type, NULL)
723 #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
724 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
725 pmbus_show_##_type, pmbus_set_##_type)
727 static void pmbus_add_boolean(struct pmbus_data *data,
728 const char *name, const char *type, int seq,
731 struct pmbus_boolean *boolean;
733 BUG_ON(data->num_booleans >= data->max_booleans);
735 boolean = &data->booleans[data->num_booleans];
737 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
739 PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx);
740 data->num_booleans++;
743 static void pmbus_add_boolean_reg(struct pmbus_data *data,
744 const char *name, const char *type,
745 int seq, int reg, int bit)
747 pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit);
750 static void pmbus_add_boolean_cmp(struct pmbus_data *data,
751 const char *name, const char *type,
752 int seq, int i1, int i2, int reg, int mask)
754 pmbus_add_boolean(data, name, type, seq,
755 (i1 << 24) | (i2 << 16) | (reg << 8) | mask);
758 static void pmbus_add_sensor(struct pmbus_data *data,
759 const char *name, const char *type, int seq,
760 int page, int reg, enum pmbus_sensor_classes class,
761 bool update, bool readonly)
763 struct pmbus_sensor *sensor;
765 BUG_ON(data->num_sensors >= data->max_sensors);
767 sensor = &data->sensors[data->num_sensors];
768 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
772 sensor->class = class;
773 sensor->update = update;
775 PMBUS_ADD_GET_ATTR(data, sensor->name, sensor,
778 PMBUS_ADD_SET_ATTR(data, sensor->name, sensor,
783 static void pmbus_add_label(struct pmbus_data *data,
784 const char *name, int seq,
785 const char *lstring, int index)
787 struct pmbus_label *label;
789 BUG_ON(data->num_labels >= data->max_labels);
791 label = &data->labels[data->num_labels];
792 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
794 strncpy(label->label, lstring, sizeof(label->label) - 1);
796 snprintf(label->label, sizeof(label->label), "%s%d", lstring,
799 PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels);
804 * Determine maximum number of sensors, booleans, and labels.
805 * To keep things simple, only make a rough high estimate.
807 static void pmbus_find_max_attr(struct i2c_client *client,
808 struct pmbus_data *data)
810 const struct pmbus_driver_info *info = data->info;
811 int page, max_sensors, max_booleans, max_labels;
813 max_sensors = PMBUS_MAX_INPUT_SENSORS;
814 max_booleans = PMBUS_MAX_INPUT_BOOLEANS;
815 max_labels = PMBUS_MAX_INPUT_LABELS;
817 for (page = 0; page < info->pages; page++) {
818 if (info->func[page] & PMBUS_HAVE_VOUT) {
819 max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE;
820 max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE;
823 if (info->func[page] & PMBUS_HAVE_IOUT) {
824 max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE;
825 max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE;
828 if (info->func[page] & PMBUS_HAVE_POUT) {
829 max_sensors += PMBUS_POUT_SENSORS_PER_PAGE;
830 max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE;
833 if (info->func[page] & PMBUS_HAVE_FAN12) {
834 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
835 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
837 if (info->func[page] & PMBUS_HAVE_FAN34) {
838 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
839 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
841 if (info->func[page] & PMBUS_HAVE_TEMP) {
842 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
843 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
845 if (info->func[page] & PMBUS_HAVE_TEMP2) {
846 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
847 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
849 if (info->func[page] & PMBUS_HAVE_TEMP3) {
850 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
851 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
854 data->max_sensors = max_sensors;
855 data->max_booleans = max_booleans;
856 data->max_labels = max_labels;
857 data->max_attributes = max_sensors + max_booleans + max_labels;
861 * Search for attributes. Allocate sensors, booleans, and labels as needed.
865 * The pmbus_limit_attr structure describes a single limit attribute
866 * and its associated alarm attribute.
868 struct pmbus_limit_attr {
869 u8 reg; /* Limit register */
870 const char *attr; /* Attribute name */
871 const char *alarm; /* Alarm attribute name */
872 u32 sbit; /* Alarm attribute status bit */
876 * The pmbus_sensor_attr structure describes one sensor attribute. This
877 * description includes a reference to the associated limit attributes.
879 struct pmbus_sensor_attr {
880 u8 reg; /* sensor register */
881 enum pmbus_sensor_classes class;/* sensor class */
882 const char *label; /* sensor label */
883 bool paged; /* true if paged sensor */
884 bool update; /* true if update needed */
885 bool compare; /* true if compare function needed */
886 u32 func; /* sensor mask */
887 u32 sfunc; /* sensor status mask */
888 int sbase; /* status base register */
889 u32 gbit; /* generic status bit */
890 const struct pmbus_limit_attr *limit;/* limit registers */
891 int nlimit; /* # of limit registers */
895 * Add a set of limit attributes and, if supported, the associated
898 static bool pmbus_add_limit_attrs(struct i2c_client *client,
899 struct pmbus_data *data,
900 const struct pmbus_driver_info *info,
901 const char *name, int index, int page,
903 const struct pmbus_sensor_attr *attr)
905 const struct pmbus_limit_attr *l = attr->limit;
906 int nlimit = attr->nlimit;
907 bool have_alarm = false;
910 for (i = 0; i < nlimit; i++) {
911 if (pmbus_check_word_register(client, page, l->reg)) {
912 cindex = data->num_sensors;
913 pmbus_add_sensor(data, name, l->attr, index, page,
914 l->reg, attr->class, attr->update,
916 if (info->func[page] & attr->sfunc) {
918 pmbus_add_boolean_cmp(data, name,
921 attr->sbase + page, l->sbit);
923 pmbus_add_boolean_reg(data, name,
925 attr->sbase + page, l->sbit);
935 static void pmbus_add_sensor_attrs_one(struct i2c_client *client,
936 struct pmbus_data *data,
937 const struct pmbus_driver_info *info,
940 const struct pmbus_sensor_attr *attr)
943 int cbase = data->num_sensors;
946 pmbus_add_label(data, name, index, attr->label,
947 attr->paged ? page + 1 : 0);
948 pmbus_add_sensor(data, name, "input", index, page, attr->reg,
949 attr->class, true, true);
951 have_alarm = pmbus_add_limit_attrs(client, data, info, name,
952 index, page, cbase, attr);
954 * Add generic alarm attribute only if there are no individual
955 * alarm attributes, and if there is a global alarm bit.
957 if (!have_alarm && attr->gbit)
958 pmbus_add_boolean_reg(data, name, "alarm", index,
959 PB_STATUS_BASE + page,
964 static void pmbus_add_sensor_attrs(struct i2c_client *client,
965 struct pmbus_data *data,
967 const struct pmbus_sensor_attr *attrs,
970 const struct pmbus_driver_info *info = data->info;
974 for (i = 0; i < nattrs; i++) {
977 pages = attrs->paged ? info->pages : 1;
978 for (page = 0; page < pages; page++) {
979 if (!(info->func[page] & attrs->func))
981 pmbus_add_sensor_attrs_one(client, data, info, name,
989 static const struct pmbus_limit_attr vin_limit_attrs[] = {
991 .reg = PMBUS_VIN_UV_WARN_LIMIT,
993 .alarm = "min_alarm",
994 .sbit = PB_VOLTAGE_UV_WARNING,
996 .reg = PMBUS_VIN_UV_FAULT_LIMIT,
998 .alarm = "lcrit_alarm",
999 .sbit = PB_VOLTAGE_UV_FAULT,
1001 .reg = PMBUS_VIN_OV_WARN_LIMIT,
1003 .alarm = "max_alarm",
1004 .sbit = PB_VOLTAGE_OV_WARNING,
1006 .reg = PMBUS_VIN_OV_FAULT_LIMIT,
1008 .alarm = "crit_alarm",
1009 .sbit = PB_VOLTAGE_OV_FAULT,
1013 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1015 .reg = PMBUS_VOUT_UV_WARN_LIMIT,
1017 .alarm = "min_alarm",
1018 .sbit = PB_VOLTAGE_UV_WARNING,
1020 .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1022 .alarm = "lcrit_alarm",
1023 .sbit = PB_VOLTAGE_UV_FAULT,
1025 .reg = PMBUS_VOUT_OV_WARN_LIMIT,
1027 .alarm = "max_alarm",
1028 .sbit = PB_VOLTAGE_OV_WARNING,
1030 .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1032 .alarm = "crit_alarm",
1033 .sbit = PB_VOLTAGE_OV_FAULT,
1037 static const struct pmbus_sensor_attr voltage_attributes[] = {
1039 .reg = PMBUS_READ_VIN,
1040 .class = PSC_VOLTAGE_IN,
1042 .func = PMBUS_HAVE_VIN,
1043 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1044 .sbase = PB_STATUS_INPUT_BASE,
1045 .gbit = PB_STATUS_VIN_UV,
1046 .limit = vin_limit_attrs,
1047 .nlimit = ARRAY_SIZE(vin_limit_attrs),
1049 .reg = PMBUS_READ_VCAP,
1050 .class = PSC_VOLTAGE_IN,
1052 .func = PMBUS_HAVE_VCAP,
1054 .reg = PMBUS_READ_VOUT,
1055 .class = PSC_VOLTAGE_OUT,
1058 .func = PMBUS_HAVE_VOUT,
1059 .sfunc = PMBUS_HAVE_STATUS_VOUT,
1060 .sbase = PB_STATUS_VOUT_BASE,
1061 .gbit = PB_STATUS_VOUT_OV,
1062 .limit = vout_limit_attrs,
1063 .nlimit = ARRAY_SIZE(vout_limit_attrs),
1067 /* Current attributes */
1069 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1071 .reg = PMBUS_IIN_OC_WARN_LIMIT,
1073 .alarm = "max_alarm",
1074 .sbit = PB_IIN_OC_WARNING,
1076 .reg = PMBUS_IIN_OC_FAULT_LIMIT,
1078 .alarm = "crit_alarm",
1079 .sbit = PB_IIN_OC_FAULT,
1083 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1085 .reg = PMBUS_IOUT_OC_WARN_LIMIT,
1087 .alarm = "max_alarm",
1088 .sbit = PB_IOUT_OC_WARNING,
1090 .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1092 .alarm = "lcrit_alarm",
1093 .sbit = PB_IOUT_UC_FAULT,
1095 .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1097 .alarm = "crit_alarm",
1098 .sbit = PB_IOUT_OC_FAULT,
1102 static const struct pmbus_sensor_attr current_attributes[] = {
1104 .reg = PMBUS_READ_IIN,
1105 .class = PSC_CURRENT_IN,
1107 .func = PMBUS_HAVE_IIN,
1108 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1109 .sbase = PB_STATUS_INPUT_BASE,
1110 .limit = iin_limit_attrs,
1111 .nlimit = ARRAY_SIZE(iin_limit_attrs),
1113 .reg = PMBUS_READ_IOUT,
1114 .class = PSC_CURRENT_OUT,
1117 .func = PMBUS_HAVE_IOUT,
1118 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1119 .sbase = PB_STATUS_IOUT_BASE,
1120 .gbit = PB_STATUS_IOUT_OC,
1121 .limit = iout_limit_attrs,
1122 .nlimit = ARRAY_SIZE(iout_limit_attrs),
1126 /* Power attributes */
1128 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1130 .reg = PMBUS_PIN_OP_WARN_LIMIT,
1133 .sbit = PB_PIN_OP_WARNING,
1137 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1139 .reg = PMBUS_POUT_MAX,
1141 .alarm = "cap_alarm",
1142 .sbit = PB_POWER_LIMITING,
1144 .reg = PMBUS_POUT_OP_WARN_LIMIT,
1146 .alarm = "max_alarm",
1147 .sbit = PB_POUT_OP_WARNING,
1149 .reg = PMBUS_POUT_OP_FAULT_LIMIT,
1151 .alarm = "crit_alarm",
1152 .sbit = PB_POUT_OP_FAULT,
1156 static const struct pmbus_sensor_attr power_attributes[] = {
1158 .reg = PMBUS_READ_PIN,
1161 .func = PMBUS_HAVE_PIN,
1162 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1163 .sbase = PB_STATUS_INPUT_BASE,
1164 .limit = pin_limit_attrs,
1165 .nlimit = ARRAY_SIZE(pin_limit_attrs),
1167 .reg = PMBUS_READ_POUT,
1171 .func = PMBUS_HAVE_POUT,
1172 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1173 .sbase = PB_STATUS_IOUT_BASE,
1174 .limit = pout_limit_attrs,
1175 .nlimit = ARRAY_SIZE(pout_limit_attrs),
1179 /* Temperature atributes */
1181 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1183 .reg = PMBUS_UT_WARN_LIMIT,
1185 .alarm = "min_alarm",
1186 .sbit = PB_TEMP_UT_WARNING,
1188 .reg = PMBUS_UT_FAULT_LIMIT,
1190 .alarm = "lcrit_alarm",
1191 .sbit = PB_TEMP_UT_FAULT,
1193 .reg = PMBUS_OT_WARN_LIMIT,
1195 .alarm = "max_alarm",
1196 .sbit = PB_TEMP_OT_WARNING,
1198 .reg = PMBUS_OT_FAULT_LIMIT,
1200 .alarm = "crit_alarm",
1201 .sbit = PB_TEMP_OT_FAULT,
1205 static const struct pmbus_sensor_attr temp_attributes[] = {
1207 .reg = PMBUS_READ_TEMPERATURE_1,
1208 .class = PSC_TEMPERATURE,
1212 .func = PMBUS_HAVE_TEMP,
1213 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1214 .sbase = PB_STATUS_TEMP_BASE,
1215 .gbit = PB_STATUS_TEMPERATURE,
1216 .limit = temp_limit_attrs,
1217 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1219 .reg = PMBUS_READ_TEMPERATURE_2,
1220 .class = PSC_TEMPERATURE,
1224 .func = PMBUS_HAVE_TEMP2,
1225 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1226 .sbase = PB_STATUS_TEMP_BASE,
1227 .gbit = PB_STATUS_TEMPERATURE,
1228 .limit = temp_limit_attrs,
1229 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1231 .reg = PMBUS_READ_TEMPERATURE_3,
1232 .class = PSC_TEMPERATURE,
1236 .func = PMBUS_HAVE_TEMP3,
1237 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1238 .sbase = PB_STATUS_TEMP_BASE,
1239 .gbit = PB_STATUS_TEMPERATURE,
1240 .limit = temp_limit_attrs,
1241 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1245 static const int pmbus_fan_registers[] = {
1246 PMBUS_READ_FAN_SPEED_1,
1247 PMBUS_READ_FAN_SPEED_2,
1248 PMBUS_READ_FAN_SPEED_3,
1249 PMBUS_READ_FAN_SPEED_4
1252 static const int pmbus_fan_config_registers[] = {
1253 PMBUS_FAN_CONFIG_12,
1254 PMBUS_FAN_CONFIG_12,
1255 PMBUS_FAN_CONFIG_34,
1259 static const int pmbus_fan_status_registers[] = {
1260 PMBUS_STATUS_FAN_12,
1261 PMBUS_STATUS_FAN_12,
1262 PMBUS_STATUS_FAN_34,
1266 static const u32 pmbus_fan_flags[] = {
1273 static const u32 pmbus_fan_status_flags[] = {
1274 PMBUS_HAVE_STATUS_FAN12,
1275 PMBUS_HAVE_STATUS_FAN12,
1276 PMBUS_HAVE_STATUS_FAN34,
1277 PMBUS_HAVE_STATUS_FAN34
1281 static void pmbus_add_fan_attributes(struct i2c_client *client,
1282 struct pmbus_data *data)
1284 const struct pmbus_driver_info *info = data->info;
1288 for (page = 0; page < info->pages; page++) {
1291 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1294 if (!(info->func[page] & pmbus_fan_flags[f]))
1297 if (!pmbus_check_word_register(client, page,
1298 pmbus_fan_registers[f]))
1302 * Skip fan if not installed.
1303 * Each fan configuration register covers multiple fans,
1304 * so we have to do some magic.
1306 regval = _pmbus_read_byte_data(client, page,
1307 pmbus_fan_config_registers[f]);
1309 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1312 pmbus_add_sensor(data, "fan", "input", index, page,
1313 pmbus_fan_registers[f], PSC_FAN, true,
1317 * Each fan status register covers multiple fans,
1318 * so we have to do some magic.
1320 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1321 pmbus_check_byte_register(client,
1322 page, pmbus_fan_status_registers[f])) {
1325 if (f > 1) /* fan 3, 4 */
1326 base = PB_STATUS_FAN34_BASE + page;
1328 base = PB_STATUS_FAN_BASE + page;
1329 pmbus_add_boolean_reg(data, "fan", "alarm",
1331 PB_FAN_FAN1_WARNING >> (f & 1));
1332 pmbus_add_boolean_reg(data, "fan", "fault",
1334 PB_FAN_FAN1_FAULT >> (f & 1));
1341 static void pmbus_find_attributes(struct i2c_client *client,
1342 struct pmbus_data *data)
1344 /* Voltage sensors */
1345 pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
1346 ARRAY_SIZE(voltage_attributes));
1348 /* Current sensors */
1349 pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
1350 ARRAY_SIZE(current_attributes));
1353 pmbus_add_sensor_attrs(client, data, "power", power_attributes,
1354 ARRAY_SIZE(power_attributes));
1356 /* Temperature sensors */
1357 pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
1358 ARRAY_SIZE(temp_attributes));
1361 pmbus_add_fan_attributes(client, data);
1365 * Identify chip parameters.
1366 * This function is called for all chips.
1368 static int pmbus_identify_common(struct i2c_client *client,
1369 struct pmbus_data *data)
1371 int vout_mode = -1, exponent;
1373 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
1374 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1375 if (vout_mode >= 0 && vout_mode != 0xff) {
1377 * Not all chips support the VOUT_MODE command,
1378 * so a failure to read it is not an error.
1380 switch (vout_mode >> 5) {
1381 case 0: /* linear mode */
1382 if (data->info->direct[PSC_VOLTAGE_OUT])
1385 exponent = vout_mode & 0x1f;
1386 /* and sign-extend it */
1387 if (exponent & 0x10)
1389 data->exponent = exponent;
1391 case 2: /* direct mode */
1392 if (!data->info->direct[PSC_VOLTAGE_OUT])
1400 /* Determine maximum number of sensors, booleans, and labels */
1401 pmbus_find_max_attr(client, data);
1402 pmbus_clear_fault_page(client, 0);
1406 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
1407 struct pmbus_driver_info *info)
1409 const struct pmbus_platform_data *pdata = client->dev.platform_data;
1410 struct pmbus_data *data;
1414 dev_err(&client->dev, "Missing chip information");
1418 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
1419 | I2C_FUNC_SMBUS_BYTE_DATA
1420 | I2C_FUNC_SMBUS_WORD_DATA))
1423 data = kzalloc(sizeof(*data), GFP_KERNEL);
1425 dev_err(&client->dev, "No memory to allocate driver data\n");
1429 i2c_set_clientdata(client, data);
1430 mutex_init(&data->update_lock);
1433 * Bail out if status register or PMBus revision register
1436 if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0
1437 || i2c_smbus_read_byte_data(client, PMBUS_REVISION) < 0) {
1438 dev_err(&client->dev,
1439 "Status or revision register not found\n");
1445 data->flags = pdata->flags;
1448 pmbus_clear_faults(client);
1450 if (info->identify) {
1451 ret = (*info->identify)(client, info);
1453 dev_err(&client->dev, "Chip identification failed\n");
1458 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1459 dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
1465 * Bail out if more than one page was configured, but we can not
1466 * select the highest page. This is an indication that the wrong
1467 * chip type was selected. Better bail out now than keep
1468 * returning errors later on.
1470 if (info->pages > 1 && pmbus_set_page(client, info->pages - 1) < 0) {
1471 dev_err(&client->dev, "Failed to select page %d\n",
1477 ret = pmbus_identify_common(client, data);
1479 dev_err(&client->dev, "Failed to identify chip capabilities\n");
1484 data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors,
1486 if (!data->sensors) {
1487 dev_err(&client->dev, "No memory to allocate sensor data\n");
1491 data->booleans = kzalloc(sizeof(struct pmbus_boolean)
1492 * data->max_booleans, GFP_KERNEL);
1493 if (!data->booleans) {
1494 dev_err(&client->dev, "No memory to allocate boolean data\n");
1498 data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels,
1500 if (!data->labels) {
1501 dev_err(&client->dev, "No memory to allocate label data\n");
1505 data->attributes = kzalloc(sizeof(struct attribute *)
1506 * data->max_attributes, GFP_KERNEL);
1507 if (!data->attributes) {
1508 dev_err(&client->dev, "No memory to allocate attribute data\n");
1512 pmbus_find_attributes(client, data);
1515 * If there are no attributes, something is wrong.
1516 * Bail out instead of trying to register nothing.
1518 if (!data->num_attributes) {
1519 dev_err(&client->dev, "No attributes found\n");
1521 goto out_attributes;
1524 /* Register sysfs hooks */
1525 data->group.attrs = data->attributes;
1526 ret = sysfs_create_group(&client->dev.kobj, &data->group);
1528 dev_err(&client->dev, "Failed to create sysfs entries\n");
1529 goto out_attributes;
1531 data->hwmon_dev = hwmon_device_register(&client->dev);
1532 if (IS_ERR(data->hwmon_dev)) {
1533 ret = PTR_ERR(data->hwmon_dev);
1534 dev_err(&client->dev, "Failed to register hwmon device\n");
1535 goto out_hwmon_device_register;
1539 out_hwmon_device_register:
1540 sysfs_remove_group(&client->dev.kobj, &data->group);
1542 kfree(data->attributes);
1544 kfree(data->labels);
1546 kfree(data->booleans);
1548 kfree(data->sensors);
1553 EXPORT_SYMBOL_GPL(pmbus_do_probe);
1555 int pmbus_do_remove(struct i2c_client *client)
1557 struct pmbus_data *data = i2c_get_clientdata(client);
1558 hwmon_device_unregister(data->hwmon_dev);
1559 sysfs_remove_group(&client->dev.kobj, &data->group);
1560 kfree(data->attributes);
1561 kfree(data->labels);
1562 kfree(data->booleans);
1563 kfree(data->sensors);
1567 EXPORT_SYMBOL_GPL(pmbus_do_remove);
1569 MODULE_AUTHOR("Guenter Roeck");
1570 MODULE_DESCRIPTION("PMBus core driver");
1571 MODULE_LICENSE("GPL");