Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / hwmon / pmbus / pmbus_core.c
blobd89b33967a852b8975f09cb5cea88371f1d52e85
1 /*
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>
31 #include "pmbus.h"
34 * Constants needed to determine number of sensors, booleans, and labels.
36 #define PMBUS_MAX_INPUT_SENSORS 22 /* 10*volt, 7*curr, 5*power */
37 #define PMBUS_VOUT_SENSORS_PER_PAGE 9 /* input, min, max, lcrit,
38 crit, lowest, highest, avg,
39 reset */
40 #define PMBUS_IOUT_SENSORS_PER_PAGE 8 /* input, min, max, crit,
41 lowest, highest, avg,
42 reset */
43 #define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */
44 #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
45 #define PMBUS_MAX_SENSORS_PER_TEMP 8 /* input, min, max, lcrit,
46 crit, lowest, highest,
47 reset */
49 #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
50 lcrit_alarm, crit_alarm;
51 c: alarm, crit_alarm;
52 p: crit_alarm */
53 #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
54 lcrit_alarm, crit_alarm */
55 #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
56 crit_alarm */
57 #define PMBUS_POUT_BOOLEANS_PER_PAGE 3 /* cap_alarm, alarm, crit_alarm
59 #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
60 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
61 lcrit_alarm, crit_alarm */
63 #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
66 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
67 * are paged. status_input is unpaged.
69 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
72 * Index into status register array, per status register group
74 #define PB_STATUS_BASE 0
75 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
76 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
77 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
78 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
79 #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
80 #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
82 #define PMBUS_NAME_SIZE 24
84 struct pmbus_sensor {
85 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */
86 struct sensor_device_attribute attribute;
87 u8 page; /* page number */
88 u16 reg; /* register */
89 enum pmbus_sensor_classes class; /* sensor class */
90 bool update; /* runtime sensor update needed */
91 int data; /* Sensor data.
92 Negative if there was a read error */
95 struct pmbus_boolean {
96 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */
97 struct sensor_device_attribute attribute;
100 struct pmbus_label {
101 char name[PMBUS_NAME_SIZE]; /* sysfs label name */
102 struct sensor_device_attribute attribute;
103 char label[PMBUS_NAME_SIZE]; /* label */
106 struct pmbus_data {
107 struct device *hwmon_dev;
109 u32 flags; /* from platform data */
111 int exponent; /* linear mode: exponent for output voltages */
113 const struct pmbus_driver_info *info;
115 int max_attributes;
116 int num_attributes;
117 struct attribute **attributes;
118 struct attribute_group group;
121 * Sensors cover both sensor and limit registers.
123 int max_sensors;
124 int num_sensors;
125 struct pmbus_sensor *sensors;
127 * Booleans are used for alarms.
128 * Values are determined from status registers.
130 int max_booleans;
131 int num_booleans;
132 struct pmbus_boolean *booleans;
134 * Labels are used to map generic names (e.g., "in1")
135 * to PMBus specific names (e.g., "vin" or "vout1").
137 int max_labels;
138 int num_labels;
139 struct pmbus_label *labels;
141 struct mutex update_lock;
142 bool valid;
143 unsigned long last_updated; /* in jiffies */
146 * A single status register covers multiple attributes,
147 * so we keep them all together.
149 u8 status[PB_NUM_STATUS_REG];
151 u8 currpage;
154 int pmbus_set_page(struct i2c_client *client, u8 page)
156 struct pmbus_data *data = i2c_get_clientdata(client);
157 int rv = 0;
158 int newpage;
160 if (page != data->currpage) {
161 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
162 newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
163 if (newpage != page)
164 rv = -EIO;
165 else
166 data->currpage = page;
168 return rv;
170 EXPORT_SYMBOL_GPL(pmbus_set_page);
172 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
174 int rv;
176 if (page >= 0) {
177 rv = pmbus_set_page(client, page);
178 if (rv < 0)
179 return rv;
182 return i2c_smbus_write_byte(client, value);
184 EXPORT_SYMBOL_GPL(pmbus_write_byte);
187 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
188 * a device specific mapping funcion exists and calls it if necessary.
190 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
192 struct pmbus_data *data = i2c_get_clientdata(client);
193 const struct pmbus_driver_info *info = data->info;
194 int status;
196 if (info->write_byte) {
197 status = info->write_byte(client, page, value);
198 if (status != -ENODATA)
199 return status;
201 return pmbus_write_byte(client, page, value);
204 int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word)
206 int rv;
208 rv = pmbus_set_page(client, page);
209 if (rv < 0)
210 return rv;
212 return i2c_smbus_write_word_data(client, reg, word);
214 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
217 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
218 * a device specific mapping function exists and calls it if necessary.
220 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
221 u16 word)
223 struct pmbus_data *data = i2c_get_clientdata(client);
224 const struct pmbus_driver_info *info = data->info;
225 int status;
227 if (info->write_word_data) {
228 status = info->write_word_data(client, page, reg, word);
229 if (status != -ENODATA)
230 return status;
232 if (reg >= PMBUS_VIRT_BASE)
233 return -ENXIO;
234 return pmbus_write_word_data(client, page, reg, word);
237 int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
239 int rv;
241 rv = pmbus_set_page(client, page);
242 if (rv < 0)
243 return rv;
245 return i2c_smbus_read_word_data(client, reg);
247 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
250 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
251 * a device specific mapping function exists and calls it if necessary.
253 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
255 struct pmbus_data *data = i2c_get_clientdata(client);
256 const struct pmbus_driver_info *info = data->info;
257 int status;
259 if (info->read_word_data) {
260 status = info->read_word_data(client, page, reg);
261 if (status != -ENODATA)
262 return status;
264 if (reg >= PMBUS_VIRT_BASE)
265 return -ENXIO;
266 return pmbus_read_word_data(client, page, reg);
269 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
271 int rv;
273 if (page >= 0) {
274 rv = pmbus_set_page(client, page);
275 if (rv < 0)
276 return rv;
279 return i2c_smbus_read_byte_data(client, reg);
281 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
284 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
285 * a device specific mapping function exists and calls it if necessary.
287 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
289 struct pmbus_data *data = i2c_get_clientdata(client);
290 const struct pmbus_driver_info *info = data->info;
291 int status;
293 if (info->read_byte_data) {
294 status = info->read_byte_data(client, page, reg);
295 if (status != -ENODATA)
296 return status;
298 return pmbus_read_byte_data(client, page, reg);
301 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
303 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
306 void pmbus_clear_faults(struct i2c_client *client)
308 struct pmbus_data *data = i2c_get_clientdata(client);
309 int i;
311 for (i = 0; i < data->info->pages; i++)
312 pmbus_clear_fault_page(client, i);
314 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
316 static int pmbus_check_status_cml(struct i2c_client *client)
318 int status, status2;
320 status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_BYTE);
321 if (status < 0 || (status & PB_STATUS_CML)) {
322 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
323 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
324 return -EIO;
326 return 0;
329 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
331 int rv;
332 struct pmbus_data *data = i2c_get_clientdata(client);
334 rv = _pmbus_read_byte_data(client, page, reg);
335 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
336 rv = pmbus_check_status_cml(client);
337 pmbus_clear_fault_page(client, -1);
338 return rv >= 0;
340 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
342 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
344 int rv;
345 struct pmbus_data *data = i2c_get_clientdata(client);
347 rv = _pmbus_read_word_data(client, page, reg);
348 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
349 rv = pmbus_check_status_cml(client);
350 pmbus_clear_fault_page(client, -1);
351 return rv >= 0;
353 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
355 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
357 struct pmbus_data *data = i2c_get_clientdata(client);
359 return data->info;
361 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
363 static struct pmbus_data *pmbus_update_device(struct device *dev)
365 struct i2c_client *client = to_i2c_client(dev);
366 struct pmbus_data *data = i2c_get_clientdata(client);
367 const struct pmbus_driver_info *info = data->info;
369 mutex_lock(&data->update_lock);
370 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
371 int i;
373 for (i = 0; i < info->pages; i++)
374 data->status[PB_STATUS_BASE + i]
375 = _pmbus_read_byte_data(client, i,
376 PMBUS_STATUS_BYTE);
377 for (i = 0; i < info->pages; i++) {
378 if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT))
379 continue;
380 data->status[PB_STATUS_VOUT_BASE + i]
381 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_VOUT);
383 for (i = 0; i < info->pages; i++) {
384 if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT))
385 continue;
386 data->status[PB_STATUS_IOUT_BASE + i]
387 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_IOUT);
389 for (i = 0; i < info->pages; i++) {
390 if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP))
391 continue;
392 data->status[PB_STATUS_TEMP_BASE + i]
393 = _pmbus_read_byte_data(client, i,
394 PMBUS_STATUS_TEMPERATURE);
396 for (i = 0; i < info->pages; i++) {
397 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12))
398 continue;
399 data->status[PB_STATUS_FAN_BASE + i]
400 = _pmbus_read_byte_data(client, i,
401 PMBUS_STATUS_FAN_12);
404 for (i = 0; i < info->pages; i++) {
405 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34))
406 continue;
407 data->status[PB_STATUS_FAN34_BASE + i]
408 = _pmbus_read_byte_data(client, i,
409 PMBUS_STATUS_FAN_34);
412 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
413 data->status[PB_STATUS_INPUT_BASE]
414 = _pmbus_read_byte_data(client, 0,
415 PMBUS_STATUS_INPUT);
417 for (i = 0; i < data->num_sensors; i++) {
418 struct pmbus_sensor *sensor = &data->sensors[i];
420 if (!data->valid || sensor->update)
421 sensor->data
422 = _pmbus_read_word_data(client,
423 sensor->page,
424 sensor->reg);
426 pmbus_clear_faults(client);
427 data->last_updated = jiffies;
428 data->valid = 1;
430 mutex_unlock(&data->update_lock);
431 return data;
435 * Convert linear sensor values to milli- or micro-units
436 * depending on sensor type.
438 static long pmbus_reg2data_linear(struct pmbus_data *data,
439 struct pmbus_sensor *sensor)
441 s16 exponent;
442 s32 mantissa;
443 long val;
445 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
446 exponent = data->exponent;
447 mantissa = (u16) sensor->data;
448 } else { /* LINEAR11 */
449 exponent = ((s16)sensor->data) >> 11;
450 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
453 val = mantissa;
455 /* scale result to milli-units for all sensors except fans */
456 if (sensor->class != PSC_FAN)
457 val = val * 1000L;
459 /* scale result to micro-units for power sensors */
460 if (sensor->class == PSC_POWER)
461 val = val * 1000L;
463 if (exponent >= 0)
464 val <<= exponent;
465 else
466 val >>= -exponent;
468 return val;
472 * Convert direct sensor values to milli- or micro-units
473 * depending on sensor type.
475 static long pmbus_reg2data_direct(struct pmbus_data *data,
476 struct pmbus_sensor *sensor)
478 long val = (s16) sensor->data;
479 long m, b, R;
481 m = data->info->m[sensor->class];
482 b = data->info->b[sensor->class];
483 R = data->info->R[sensor->class];
485 if (m == 0)
486 return 0;
488 /* X = 1/m * (Y * 10^-R - b) */
489 R = -R;
490 /* scale result to milli-units for everything but fans */
491 if (sensor->class != PSC_FAN) {
492 R += 3;
493 b *= 1000;
496 /* scale result to micro-units for power sensors */
497 if (sensor->class == PSC_POWER) {
498 R += 3;
499 b *= 1000;
502 while (R > 0) {
503 val *= 10;
504 R--;
506 while (R < 0) {
507 val = DIV_ROUND_CLOSEST(val, 10);
508 R++;
511 return (val - b) / m;
515 * Convert VID sensor values to milli- or micro-units
516 * depending on sensor type.
517 * We currently only support VR11.
519 static long pmbus_reg2data_vid(struct pmbus_data *data,
520 struct pmbus_sensor *sensor)
522 long val = sensor->data;
524 if (val < 0x02 || val > 0xb2)
525 return 0;
526 return DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
529 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
531 long val;
533 switch (data->info->format[sensor->class]) {
534 case direct:
535 val = pmbus_reg2data_direct(data, sensor);
536 break;
537 case vid:
538 val = pmbus_reg2data_vid(data, sensor);
539 break;
540 case linear:
541 default:
542 val = pmbus_reg2data_linear(data, sensor);
543 break;
545 return val;
548 #define MAX_MANTISSA (1023 * 1000)
549 #define MIN_MANTISSA (511 * 1000)
551 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
552 enum pmbus_sensor_classes class, long val)
554 s16 exponent = 0, mantissa;
555 bool negative = false;
557 /* simple case */
558 if (val == 0)
559 return 0;
561 if (class == PSC_VOLTAGE_OUT) {
562 /* LINEAR16 does not support negative voltages */
563 if (val < 0)
564 return 0;
567 * For a static exponents, we don't have a choice
568 * but to adjust the value to it.
570 if (data->exponent < 0)
571 val <<= -data->exponent;
572 else
573 val >>= data->exponent;
574 val = DIV_ROUND_CLOSEST(val, 1000);
575 return val & 0xffff;
578 if (val < 0) {
579 negative = true;
580 val = -val;
583 /* Power is in uW. Convert to mW before converting. */
584 if (class == PSC_POWER)
585 val = DIV_ROUND_CLOSEST(val, 1000L);
588 * For simplicity, convert fan data to milli-units
589 * before calculating the exponent.
591 if (class == PSC_FAN)
592 val = val * 1000;
594 /* Reduce large mantissa until it fits into 10 bit */
595 while (val >= MAX_MANTISSA && exponent < 15) {
596 exponent++;
597 val >>= 1;
599 /* Increase small mantissa to improve precision */
600 while (val < MIN_MANTISSA && exponent > -15) {
601 exponent--;
602 val <<= 1;
605 /* Convert mantissa from milli-units to units */
606 mantissa = DIV_ROUND_CLOSEST(val, 1000);
608 /* Ensure that resulting number is within range */
609 if (mantissa > 0x3ff)
610 mantissa = 0x3ff;
612 /* restore sign */
613 if (negative)
614 mantissa = -mantissa;
616 /* Convert to 5 bit exponent, 11 bit mantissa */
617 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
620 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
621 enum pmbus_sensor_classes class, long val)
623 long m, b, R;
625 m = data->info->m[class];
626 b = data->info->b[class];
627 R = data->info->R[class];
629 /* Power is in uW. Adjust R and b. */
630 if (class == PSC_POWER) {
631 R -= 3;
632 b *= 1000;
635 /* Calculate Y = (m * X + b) * 10^R */
636 if (class != PSC_FAN) {
637 R -= 3; /* Adjust R and b for data in milli-units */
638 b *= 1000;
640 val = val * m + b;
642 while (R > 0) {
643 val *= 10;
644 R--;
646 while (R < 0) {
647 val = DIV_ROUND_CLOSEST(val, 10);
648 R++;
651 return val;
654 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
655 enum pmbus_sensor_classes class, long val)
657 val = SENSORS_LIMIT(val, 500, 1600);
659 return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
662 static u16 pmbus_data2reg(struct pmbus_data *data,
663 enum pmbus_sensor_classes class, long val)
665 u16 regval;
667 switch (data->info->format[class]) {
668 case direct:
669 regval = pmbus_data2reg_direct(data, class, val);
670 break;
671 case vid:
672 regval = pmbus_data2reg_vid(data, class, val);
673 break;
674 case linear:
675 default:
676 regval = pmbus_data2reg_linear(data, class, val);
677 break;
679 return regval;
683 * Return boolean calculated from converted data.
684 * <index> defines a status register index and mask, and optionally
685 * two sensor indexes.
686 * The upper half-word references the two sensors,
687 * two sensor indices.
688 * The upper half-word references the two optional sensors,
689 * the lower half word references status register and mask.
690 * The function returns true if (status[reg] & mask) is true and,
691 * if specified, if v1 >= v2.
692 * To determine if an object exceeds upper limits, specify <v, limit>.
693 * To determine if an object exceeds lower limits, specify <limit, v>.
695 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
696 * index are set. s1 and s2 (the sensor index values) are zero in this case.
697 * The function returns true if (status[reg] & mask) is true.
699 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
700 * a specified limit has to be performed to determine the boolean result.
701 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
702 * sensor values referenced by sensor indices s1 and s2).
704 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
705 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
707 * If a negative value is stored in any of the referenced registers, this value
708 * reflects an error code which will be returned.
710 static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val)
712 u8 s1 = (index >> 24) & 0xff;
713 u8 s2 = (index >> 16) & 0xff;
714 u8 reg = (index >> 8) & 0xff;
715 u8 mask = index & 0xff;
716 int status;
717 u8 regval;
719 status = data->status[reg];
720 if (status < 0)
721 return status;
723 regval = status & mask;
724 if (!s1 && !s2)
725 *val = !!regval;
726 else {
727 long v1, v2;
728 struct pmbus_sensor *sensor1, *sensor2;
730 sensor1 = &data->sensors[s1];
731 if (sensor1->data < 0)
732 return sensor1->data;
733 sensor2 = &data->sensors[s2];
734 if (sensor2->data < 0)
735 return sensor2->data;
737 v1 = pmbus_reg2data(data, sensor1);
738 v2 = pmbus_reg2data(data, sensor2);
739 *val = !!(regval && v1 >= v2);
741 return 0;
744 static ssize_t pmbus_show_boolean(struct device *dev,
745 struct device_attribute *da, char *buf)
747 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
748 struct pmbus_data *data = pmbus_update_device(dev);
749 int val;
750 int err;
752 err = pmbus_get_boolean(data, attr->index, &val);
753 if (err)
754 return err;
755 return snprintf(buf, PAGE_SIZE, "%d\n", val);
758 static ssize_t pmbus_show_sensor(struct device *dev,
759 struct device_attribute *da, char *buf)
761 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
762 struct pmbus_data *data = pmbus_update_device(dev);
763 struct pmbus_sensor *sensor;
765 sensor = &data->sensors[attr->index];
766 if (sensor->data < 0)
767 return sensor->data;
769 return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
772 static ssize_t pmbus_set_sensor(struct device *dev,
773 struct device_attribute *devattr,
774 const char *buf, size_t count)
776 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
777 struct i2c_client *client = to_i2c_client(dev);
778 struct pmbus_data *data = i2c_get_clientdata(client);
779 struct pmbus_sensor *sensor = &data->sensors[attr->index];
780 ssize_t rv = count;
781 long val = 0;
782 int ret;
783 u16 regval;
785 if (strict_strtol(buf, 10, &val) < 0)
786 return -EINVAL;
788 mutex_lock(&data->update_lock);
789 regval = pmbus_data2reg(data, sensor->class, val);
790 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
791 if (ret < 0)
792 rv = ret;
793 else
794 data->sensors[attr->index].data = regval;
795 mutex_unlock(&data->update_lock);
796 return rv;
799 static ssize_t pmbus_show_label(struct device *dev,
800 struct device_attribute *da, char *buf)
802 struct i2c_client *client = to_i2c_client(dev);
803 struct pmbus_data *data = i2c_get_clientdata(client);
804 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
806 return snprintf(buf, PAGE_SIZE, "%s\n",
807 data->labels[attr->index].label);
810 #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
811 do { \
812 struct sensor_device_attribute *a \
813 = &data->_type##s[data->num_##_type##s].attribute; \
814 BUG_ON(data->num_attributes >= data->max_attributes); \
815 sysfs_attr_init(&a->dev_attr.attr); \
816 a->dev_attr.attr.name = _name; \
817 a->dev_attr.attr.mode = _mode; \
818 a->dev_attr.show = _show; \
819 a->dev_attr.store = _set; \
820 a->index = _idx; \
821 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
822 data->num_attributes++; \
823 } while (0)
825 #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
826 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
827 pmbus_show_##_type, NULL)
829 #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
830 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
831 pmbus_show_##_type, pmbus_set_##_type)
833 static void pmbus_add_boolean(struct pmbus_data *data,
834 const char *name, const char *type, int seq,
835 int idx)
837 struct pmbus_boolean *boolean;
839 BUG_ON(data->num_booleans >= data->max_booleans);
841 boolean = &data->booleans[data->num_booleans];
843 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
844 name, seq, type);
845 PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx);
846 data->num_booleans++;
849 static void pmbus_add_boolean_reg(struct pmbus_data *data,
850 const char *name, const char *type,
851 int seq, int reg, int bit)
853 pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit);
856 static void pmbus_add_boolean_cmp(struct pmbus_data *data,
857 const char *name, const char *type,
858 int seq, int i1, int i2, int reg, int mask)
860 pmbus_add_boolean(data, name, type, seq,
861 (i1 << 24) | (i2 << 16) | (reg << 8) | mask);
864 static void pmbus_add_sensor(struct pmbus_data *data,
865 const char *name, const char *type, int seq,
866 int page, int reg, enum pmbus_sensor_classes class,
867 bool update, bool readonly)
869 struct pmbus_sensor *sensor;
871 BUG_ON(data->num_sensors >= data->max_sensors);
873 sensor = &data->sensors[data->num_sensors];
874 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
875 name, seq, type);
876 sensor->page = page;
877 sensor->reg = reg;
878 sensor->class = class;
879 sensor->update = update;
880 if (readonly)
881 PMBUS_ADD_GET_ATTR(data, sensor->name, sensor,
882 data->num_sensors);
883 else
884 PMBUS_ADD_SET_ATTR(data, sensor->name, sensor,
885 data->num_sensors);
886 data->num_sensors++;
889 static void pmbus_add_label(struct pmbus_data *data,
890 const char *name, int seq,
891 const char *lstring, int index)
893 struct pmbus_label *label;
895 BUG_ON(data->num_labels >= data->max_labels);
897 label = &data->labels[data->num_labels];
898 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
899 if (!index)
900 strncpy(label->label, lstring, sizeof(label->label) - 1);
901 else
902 snprintf(label->label, sizeof(label->label), "%s%d", lstring,
903 index);
905 PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels);
906 data->num_labels++;
910 * Determine maximum number of sensors, booleans, and labels.
911 * To keep things simple, only make a rough high estimate.
913 static void pmbus_find_max_attr(struct i2c_client *client,
914 struct pmbus_data *data)
916 const struct pmbus_driver_info *info = data->info;
917 int page, max_sensors, max_booleans, max_labels;
919 max_sensors = PMBUS_MAX_INPUT_SENSORS;
920 max_booleans = PMBUS_MAX_INPUT_BOOLEANS;
921 max_labels = PMBUS_MAX_INPUT_LABELS;
923 for (page = 0; page < info->pages; page++) {
924 if (info->func[page] & PMBUS_HAVE_VOUT) {
925 max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE;
926 max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE;
927 max_labels++;
929 if (info->func[page] & PMBUS_HAVE_IOUT) {
930 max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE;
931 max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE;
932 max_labels++;
934 if (info->func[page] & PMBUS_HAVE_POUT) {
935 max_sensors += PMBUS_POUT_SENSORS_PER_PAGE;
936 max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE;
937 max_labels++;
939 if (info->func[page] & PMBUS_HAVE_FAN12) {
940 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
941 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
943 if (info->func[page] & PMBUS_HAVE_FAN34) {
944 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
945 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
947 if (info->func[page] & PMBUS_HAVE_TEMP) {
948 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
949 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
951 if (info->func[page] & PMBUS_HAVE_TEMP2) {
952 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
953 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
955 if (info->func[page] & PMBUS_HAVE_TEMP3) {
956 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
957 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
960 data->max_sensors = max_sensors;
961 data->max_booleans = max_booleans;
962 data->max_labels = max_labels;
963 data->max_attributes = max_sensors + max_booleans + max_labels;
967 * Search for attributes. Allocate sensors, booleans, and labels as needed.
971 * The pmbus_limit_attr structure describes a single limit attribute
972 * and its associated alarm attribute.
974 struct pmbus_limit_attr {
975 u16 reg; /* Limit register */
976 bool update; /* True if register needs updates */
977 bool low; /* True if low limit; for limits with compare
978 functions only */
979 const char *attr; /* Attribute name */
980 const char *alarm; /* Alarm attribute name */
981 u32 sbit; /* Alarm attribute status bit */
985 * The pmbus_sensor_attr structure describes one sensor attribute. This
986 * description includes a reference to the associated limit attributes.
988 struct pmbus_sensor_attr {
989 u8 reg; /* sensor register */
990 enum pmbus_sensor_classes class;/* sensor class */
991 const char *label; /* sensor label */
992 bool paged; /* true if paged sensor */
993 bool update; /* true if update needed */
994 bool compare; /* true if compare function needed */
995 u32 func; /* sensor mask */
996 u32 sfunc; /* sensor status mask */
997 int sbase; /* status base register */
998 u32 gbit; /* generic status bit */
999 const struct pmbus_limit_attr *limit;/* limit registers */
1000 int nlimit; /* # of limit registers */
1004 * Add a set of limit attributes and, if supported, the associated
1005 * alarm attributes.
1007 static bool pmbus_add_limit_attrs(struct i2c_client *client,
1008 struct pmbus_data *data,
1009 const struct pmbus_driver_info *info,
1010 const char *name, int index, int page,
1011 int cbase,
1012 const struct pmbus_sensor_attr *attr)
1014 const struct pmbus_limit_attr *l = attr->limit;
1015 int nlimit = attr->nlimit;
1016 bool have_alarm = false;
1017 int i, cindex;
1019 for (i = 0; i < nlimit; i++) {
1020 if (pmbus_check_word_register(client, page, l->reg)) {
1021 cindex = data->num_sensors;
1022 pmbus_add_sensor(data, name, l->attr, index, page,
1023 l->reg, attr->class,
1024 attr->update || l->update,
1025 false);
1026 if (l->sbit && (info->func[page] & attr->sfunc)) {
1027 if (attr->compare) {
1028 pmbus_add_boolean_cmp(data, name,
1029 l->alarm, index,
1030 l->low ? cindex : cbase,
1031 l->low ? cbase : cindex,
1032 attr->sbase + page, l->sbit);
1033 } else {
1034 pmbus_add_boolean_reg(data, name,
1035 l->alarm, index,
1036 attr->sbase + page, l->sbit);
1038 have_alarm = true;
1041 l++;
1043 return have_alarm;
1046 static void pmbus_add_sensor_attrs_one(struct i2c_client *client,
1047 struct pmbus_data *data,
1048 const struct pmbus_driver_info *info,
1049 const char *name,
1050 int index, int page,
1051 const struct pmbus_sensor_attr *attr)
1053 bool have_alarm;
1054 int cbase = data->num_sensors;
1056 if (attr->label)
1057 pmbus_add_label(data, name, index, attr->label,
1058 attr->paged ? page + 1 : 0);
1059 pmbus_add_sensor(data, name, "input", index, page, attr->reg,
1060 attr->class, true, true);
1061 if (attr->sfunc) {
1062 have_alarm = pmbus_add_limit_attrs(client, data, info, name,
1063 index, page, cbase, attr);
1065 * Add generic alarm attribute only if there are no individual
1066 * alarm attributes, if there is a global alarm bit, and if
1067 * the generic status register for this page is accessible.
1069 if (!have_alarm && attr->gbit &&
1070 pmbus_check_byte_register(client, page, PMBUS_STATUS_BYTE))
1071 pmbus_add_boolean_reg(data, name, "alarm", index,
1072 PB_STATUS_BASE + page,
1073 attr->gbit);
1077 static void pmbus_add_sensor_attrs(struct i2c_client *client,
1078 struct pmbus_data *data,
1079 const char *name,
1080 const struct pmbus_sensor_attr *attrs,
1081 int nattrs)
1083 const struct pmbus_driver_info *info = data->info;
1084 int index, i;
1086 index = 1;
1087 for (i = 0; i < nattrs; i++) {
1088 int page, pages;
1090 pages = attrs->paged ? info->pages : 1;
1091 for (page = 0; page < pages; page++) {
1092 if (!(info->func[page] & attrs->func))
1093 continue;
1094 pmbus_add_sensor_attrs_one(client, data, info, name,
1095 index, page, attrs);
1096 index++;
1098 attrs++;
1102 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1104 .reg = PMBUS_VIN_UV_WARN_LIMIT,
1105 .attr = "min",
1106 .alarm = "min_alarm",
1107 .sbit = PB_VOLTAGE_UV_WARNING,
1108 }, {
1109 .reg = PMBUS_VIN_UV_FAULT_LIMIT,
1110 .attr = "lcrit",
1111 .alarm = "lcrit_alarm",
1112 .sbit = PB_VOLTAGE_UV_FAULT,
1113 }, {
1114 .reg = PMBUS_VIN_OV_WARN_LIMIT,
1115 .attr = "max",
1116 .alarm = "max_alarm",
1117 .sbit = PB_VOLTAGE_OV_WARNING,
1118 }, {
1119 .reg = PMBUS_VIN_OV_FAULT_LIMIT,
1120 .attr = "crit",
1121 .alarm = "crit_alarm",
1122 .sbit = PB_VOLTAGE_OV_FAULT,
1123 }, {
1124 .reg = PMBUS_VIRT_READ_VIN_AVG,
1125 .update = true,
1126 .attr = "average",
1127 }, {
1128 .reg = PMBUS_VIRT_READ_VIN_MIN,
1129 .update = true,
1130 .attr = "lowest",
1131 }, {
1132 .reg = PMBUS_VIRT_READ_VIN_MAX,
1133 .update = true,
1134 .attr = "highest",
1135 }, {
1136 .reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1137 .attr = "reset_history",
1141 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1143 .reg = PMBUS_VOUT_UV_WARN_LIMIT,
1144 .attr = "min",
1145 .alarm = "min_alarm",
1146 .sbit = PB_VOLTAGE_UV_WARNING,
1147 }, {
1148 .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1149 .attr = "lcrit",
1150 .alarm = "lcrit_alarm",
1151 .sbit = PB_VOLTAGE_UV_FAULT,
1152 }, {
1153 .reg = PMBUS_VOUT_OV_WARN_LIMIT,
1154 .attr = "max",
1155 .alarm = "max_alarm",
1156 .sbit = PB_VOLTAGE_OV_WARNING,
1157 }, {
1158 .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1159 .attr = "crit",
1160 .alarm = "crit_alarm",
1161 .sbit = PB_VOLTAGE_OV_FAULT,
1162 }, {
1163 .reg = PMBUS_VIRT_READ_VOUT_AVG,
1164 .update = true,
1165 .attr = "average",
1166 }, {
1167 .reg = PMBUS_VIRT_READ_VOUT_MIN,
1168 .update = true,
1169 .attr = "lowest",
1170 }, {
1171 .reg = PMBUS_VIRT_READ_VOUT_MAX,
1172 .update = true,
1173 .attr = "highest",
1174 }, {
1175 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1176 .attr = "reset_history",
1180 static const struct pmbus_sensor_attr voltage_attributes[] = {
1182 .reg = PMBUS_READ_VIN,
1183 .class = PSC_VOLTAGE_IN,
1184 .label = "vin",
1185 .func = PMBUS_HAVE_VIN,
1186 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1187 .sbase = PB_STATUS_INPUT_BASE,
1188 .gbit = PB_STATUS_VIN_UV,
1189 .limit = vin_limit_attrs,
1190 .nlimit = ARRAY_SIZE(vin_limit_attrs),
1191 }, {
1192 .reg = PMBUS_READ_VCAP,
1193 .class = PSC_VOLTAGE_IN,
1194 .label = "vcap",
1195 .func = PMBUS_HAVE_VCAP,
1196 }, {
1197 .reg = PMBUS_READ_VOUT,
1198 .class = PSC_VOLTAGE_OUT,
1199 .label = "vout",
1200 .paged = true,
1201 .func = PMBUS_HAVE_VOUT,
1202 .sfunc = PMBUS_HAVE_STATUS_VOUT,
1203 .sbase = PB_STATUS_VOUT_BASE,
1204 .gbit = PB_STATUS_VOUT_OV,
1205 .limit = vout_limit_attrs,
1206 .nlimit = ARRAY_SIZE(vout_limit_attrs),
1210 /* Current attributes */
1212 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1214 .reg = PMBUS_IIN_OC_WARN_LIMIT,
1215 .attr = "max",
1216 .alarm = "max_alarm",
1217 .sbit = PB_IIN_OC_WARNING,
1218 }, {
1219 .reg = PMBUS_IIN_OC_FAULT_LIMIT,
1220 .attr = "crit",
1221 .alarm = "crit_alarm",
1222 .sbit = PB_IIN_OC_FAULT,
1223 }, {
1224 .reg = PMBUS_VIRT_READ_IIN_AVG,
1225 .update = true,
1226 .attr = "average",
1227 }, {
1228 .reg = PMBUS_VIRT_READ_IIN_MIN,
1229 .update = true,
1230 .attr = "lowest",
1231 }, {
1232 .reg = PMBUS_VIRT_READ_IIN_MAX,
1233 .update = true,
1234 .attr = "highest",
1235 }, {
1236 .reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1237 .attr = "reset_history",
1241 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1243 .reg = PMBUS_IOUT_OC_WARN_LIMIT,
1244 .attr = "max",
1245 .alarm = "max_alarm",
1246 .sbit = PB_IOUT_OC_WARNING,
1247 }, {
1248 .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1249 .attr = "lcrit",
1250 .alarm = "lcrit_alarm",
1251 .sbit = PB_IOUT_UC_FAULT,
1252 }, {
1253 .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1254 .attr = "crit",
1255 .alarm = "crit_alarm",
1256 .sbit = PB_IOUT_OC_FAULT,
1257 }, {
1258 .reg = PMBUS_VIRT_READ_IOUT_AVG,
1259 .update = true,
1260 .attr = "average",
1261 }, {
1262 .reg = PMBUS_VIRT_READ_IOUT_MIN,
1263 .update = true,
1264 .attr = "lowest",
1265 }, {
1266 .reg = PMBUS_VIRT_READ_IOUT_MAX,
1267 .update = true,
1268 .attr = "highest",
1269 }, {
1270 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1271 .attr = "reset_history",
1275 static const struct pmbus_sensor_attr current_attributes[] = {
1277 .reg = PMBUS_READ_IIN,
1278 .class = PSC_CURRENT_IN,
1279 .label = "iin",
1280 .func = PMBUS_HAVE_IIN,
1281 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1282 .sbase = PB_STATUS_INPUT_BASE,
1283 .limit = iin_limit_attrs,
1284 .nlimit = ARRAY_SIZE(iin_limit_attrs),
1285 }, {
1286 .reg = PMBUS_READ_IOUT,
1287 .class = PSC_CURRENT_OUT,
1288 .label = "iout",
1289 .paged = true,
1290 .func = PMBUS_HAVE_IOUT,
1291 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1292 .sbase = PB_STATUS_IOUT_BASE,
1293 .gbit = PB_STATUS_IOUT_OC,
1294 .limit = iout_limit_attrs,
1295 .nlimit = ARRAY_SIZE(iout_limit_attrs),
1299 /* Power attributes */
1301 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1303 .reg = PMBUS_PIN_OP_WARN_LIMIT,
1304 .attr = "max",
1305 .alarm = "alarm",
1306 .sbit = PB_PIN_OP_WARNING,
1307 }, {
1308 .reg = PMBUS_VIRT_READ_PIN_AVG,
1309 .update = true,
1310 .attr = "average",
1311 }, {
1312 .reg = PMBUS_VIRT_READ_PIN_MAX,
1313 .update = true,
1314 .attr = "input_highest",
1315 }, {
1316 .reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1317 .attr = "reset_history",
1321 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1323 .reg = PMBUS_POUT_MAX,
1324 .attr = "cap",
1325 .alarm = "cap_alarm",
1326 .sbit = PB_POWER_LIMITING,
1327 }, {
1328 .reg = PMBUS_POUT_OP_WARN_LIMIT,
1329 .attr = "max",
1330 .alarm = "max_alarm",
1331 .sbit = PB_POUT_OP_WARNING,
1332 }, {
1333 .reg = PMBUS_POUT_OP_FAULT_LIMIT,
1334 .attr = "crit",
1335 .alarm = "crit_alarm",
1336 .sbit = PB_POUT_OP_FAULT,
1340 static const struct pmbus_sensor_attr power_attributes[] = {
1342 .reg = PMBUS_READ_PIN,
1343 .class = PSC_POWER,
1344 .label = "pin",
1345 .func = PMBUS_HAVE_PIN,
1346 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1347 .sbase = PB_STATUS_INPUT_BASE,
1348 .limit = pin_limit_attrs,
1349 .nlimit = ARRAY_SIZE(pin_limit_attrs),
1350 }, {
1351 .reg = PMBUS_READ_POUT,
1352 .class = PSC_POWER,
1353 .label = "pout",
1354 .paged = true,
1355 .func = PMBUS_HAVE_POUT,
1356 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1357 .sbase = PB_STATUS_IOUT_BASE,
1358 .limit = pout_limit_attrs,
1359 .nlimit = ARRAY_SIZE(pout_limit_attrs),
1363 /* Temperature atributes */
1365 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1367 .reg = PMBUS_UT_WARN_LIMIT,
1368 .low = true,
1369 .attr = "min",
1370 .alarm = "min_alarm",
1371 .sbit = PB_TEMP_UT_WARNING,
1372 }, {
1373 .reg = PMBUS_UT_FAULT_LIMIT,
1374 .low = true,
1375 .attr = "lcrit",
1376 .alarm = "lcrit_alarm",
1377 .sbit = PB_TEMP_UT_FAULT,
1378 }, {
1379 .reg = PMBUS_OT_WARN_LIMIT,
1380 .attr = "max",
1381 .alarm = "max_alarm",
1382 .sbit = PB_TEMP_OT_WARNING,
1383 }, {
1384 .reg = PMBUS_OT_FAULT_LIMIT,
1385 .attr = "crit",
1386 .alarm = "crit_alarm",
1387 .sbit = PB_TEMP_OT_FAULT,
1388 }, {
1389 .reg = PMBUS_VIRT_READ_TEMP_MIN,
1390 .attr = "lowest",
1391 }, {
1392 .reg = PMBUS_VIRT_READ_TEMP_MAX,
1393 .attr = "highest",
1394 }, {
1395 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1396 .attr = "reset_history",
1400 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1402 .reg = PMBUS_UT_WARN_LIMIT,
1403 .low = true,
1404 .attr = "min",
1405 .alarm = "min_alarm",
1406 .sbit = PB_TEMP_UT_WARNING,
1407 }, {
1408 .reg = PMBUS_UT_FAULT_LIMIT,
1409 .low = true,
1410 .attr = "lcrit",
1411 .alarm = "lcrit_alarm",
1412 .sbit = PB_TEMP_UT_FAULT,
1413 }, {
1414 .reg = PMBUS_OT_WARN_LIMIT,
1415 .attr = "max",
1416 .alarm = "max_alarm",
1417 .sbit = PB_TEMP_OT_WARNING,
1418 }, {
1419 .reg = PMBUS_OT_FAULT_LIMIT,
1420 .attr = "crit",
1421 .alarm = "crit_alarm",
1422 .sbit = PB_TEMP_OT_FAULT,
1423 }, {
1424 .reg = PMBUS_VIRT_READ_TEMP2_MIN,
1425 .attr = "lowest",
1426 }, {
1427 .reg = PMBUS_VIRT_READ_TEMP2_MAX,
1428 .attr = "highest",
1429 }, {
1430 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1431 .attr = "reset_history",
1435 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1437 .reg = PMBUS_UT_WARN_LIMIT,
1438 .low = true,
1439 .attr = "min",
1440 .alarm = "min_alarm",
1441 .sbit = PB_TEMP_UT_WARNING,
1442 }, {
1443 .reg = PMBUS_UT_FAULT_LIMIT,
1444 .low = true,
1445 .attr = "lcrit",
1446 .alarm = "lcrit_alarm",
1447 .sbit = PB_TEMP_UT_FAULT,
1448 }, {
1449 .reg = PMBUS_OT_WARN_LIMIT,
1450 .attr = "max",
1451 .alarm = "max_alarm",
1452 .sbit = PB_TEMP_OT_WARNING,
1453 }, {
1454 .reg = PMBUS_OT_FAULT_LIMIT,
1455 .attr = "crit",
1456 .alarm = "crit_alarm",
1457 .sbit = PB_TEMP_OT_FAULT,
1461 static const struct pmbus_sensor_attr temp_attributes[] = {
1463 .reg = PMBUS_READ_TEMPERATURE_1,
1464 .class = PSC_TEMPERATURE,
1465 .paged = true,
1466 .update = true,
1467 .compare = true,
1468 .func = PMBUS_HAVE_TEMP,
1469 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1470 .sbase = PB_STATUS_TEMP_BASE,
1471 .gbit = PB_STATUS_TEMPERATURE,
1472 .limit = temp_limit_attrs,
1473 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1474 }, {
1475 .reg = PMBUS_READ_TEMPERATURE_2,
1476 .class = PSC_TEMPERATURE,
1477 .paged = true,
1478 .update = true,
1479 .compare = true,
1480 .func = PMBUS_HAVE_TEMP2,
1481 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1482 .sbase = PB_STATUS_TEMP_BASE,
1483 .gbit = PB_STATUS_TEMPERATURE,
1484 .limit = temp_limit_attrs2,
1485 .nlimit = ARRAY_SIZE(temp_limit_attrs2),
1486 }, {
1487 .reg = PMBUS_READ_TEMPERATURE_3,
1488 .class = PSC_TEMPERATURE,
1489 .paged = true,
1490 .update = true,
1491 .compare = true,
1492 .func = PMBUS_HAVE_TEMP3,
1493 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1494 .sbase = PB_STATUS_TEMP_BASE,
1495 .gbit = PB_STATUS_TEMPERATURE,
1496 .limit = temp_limit_attrs3,
1497 .nlimit = ARRAY_SIZE(temp_limit_attrs3),
1501 static const int pmbus_fan_registers[] = {
1502 PMBUS_READ_FAN_SPEED_1,
1503 PMBUS_READ_FAN_SPEED_2,
1504 PMBUS_READ_FAN_SPEED_3,
1505 PMBUS_READ_FAN_SPEED_4
1508 static const int pmbus_fan_config_registers[] = {
1509 PMBUS_FAN_CONFIG_12,
1510 PMBUS_FAN_CONFIG_12,
1511 PMBUS_FAN_CONFIG_34,
1512 PMBUS_FAN_CONFIG_34
1515 static const int pmbus_fan_status_registers[] = {
1516 PMBUS_STATUS_FAN_12,
1517 PMBUS_STATUS_FAN_12,
1518 PMBUS_STATUS_FAN_34,
1519 PMBUS_STATUS_FAN_34
1522 static const u32 pmbus_fan_flags[] = {
1523 PMBUS_HAVE_FAN12,
1524 PMBUS_HAVE_FAN12,
1525 PMBUS_HAVE_FAN34,
1526 PMBUS_HAVE_FAN34
1529 static const u32 pmbus_fan_status_flags[] = {
1530 PMBUS_HAVE_STATUS_FAN12,
1531 PMBUS_HAVE_STATUS_FAN12,
1532 PMBUS_HAVE_STATUS_FAN34,
1533 PMBUS_HAVE_STATUS_FAN34
1536 /* Fans */
1537 static void pmbus_add_fan_attributes(struct i2c_client *client,
1538 struct pmbus_data *data)
1540 const struct pmbus_driver_info *info = data->info;
1541 int index = 1;
1542 int page;
1544 for (page = 0; page < info->pages; page++) {
1545 int f;
1547 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1548 int regval;
1550 if (!(info->func[page] & pmbus_fan_flags[f]))
1551 break;
1553 if (!pmbus_check_word_register(client, page,
1554 pmbus_fan_registers[f]))
1555 break;
1558 * Skip fan if not installed.
1559 * Each fan configuration register covers multiple fans,
1560 * so we have to do some magic.
1562 regval = _pmbus_read_byte_data(client, page,
1563 pmbus_fan_config_registers[f]);
1564 if (regval < 0 ||
1565 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1566 continue;
1568 pmbus_add_sensor(data, "fan", "input", index, page,
1569 pmbus_fan_registers[f], PSC_FAN, true,
1570 true);
1573 * Each fan status register covers multiple fans,
1574 * so we have to do some magic.
1576 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1577 pmbus_check_byte_register(client,
1578 page, pmbus_fan_status_registers[f])) {
1579 int base;
1581 if (f > 1) /* fan 3, 4 */
1582 base = PB_STATUS_FAN34_BASE + page;
1583 else
1584 base = PB_STATUS_FAN_BASE + page;
1585 pmbus_add_boolean_reg(data, "fan", "alarm",
1586 index, base,
1587 PB_FAN_FAN1_WARNING >> (f & 1));
1588 pmbus_add_boolean_reg(data, "fan", "fault",
1589 index, base,
1590 PB_FAN_FAN1_FAULT >> (f & 1));
1592 index++;
1597 static void pmbus_find_attributes(struct i2c_client *client,
1598 struct pmbus_data *data)
1600 /* Voltage sensors */
1601 pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
1602 ARRAY_SIZE(voltage_attributes));
1604 /* Current sensors */
1605 pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
1606 ARRAY_SIZE(current_attributes));
1608 /* Power sensors */
1609 pmbus_add_sensor_attrs(client, data, "power", power_attributes,
1610 ARRAY_SIZE(power_attributes));
1612 /* Temperature sensors */
1613 pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
1614 ARRAY_SIZE(temp_attributes));
1616 /* Fans */
1617 pmbus_add_fan_attributes(client, data);
1621 * Identify chip parameters.
1622 * This function is called for all chips.
1624 static int pmbus_identify_common(struct i2c_client *client,
1625 struct pmbus_data *data)
1627 int vout_mode = -1;
1629 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
1630 vout_mode = _pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1631 if (vout_mode >= 0 && vout_mode != 0xff) {
1633 * Not all chips support the VOUT_MODE command,
1634 * so a failure to read it is not an error.
1636 switch (vout_mode >> 5) {
1637 case 0: /* linear mode */
1638 if (data->info->format[PSC_VOLTAGE_OUT] != linear)
1639 return -ENODEV;
1641 data->exponent = ((s8)(vout_mode << 3)) >> 3;
1642 break;
1643 case 1: /* VID mode */
1644 if (data->info->format[PSC_VOLTAGE_OUT] != vid)
1645 return -ENODEV;
1646 break;
1647 case 2: /* direct mode */
1648 if (data->info->format[PSC_VOLTAGE_OUT] != direct)
1649 return -ENODEV;
1650 break;
1651 default:
1652 return -ENODEV;
1656 /* Determine maximum number of sensors, booleans, and labels */
1657 pmbus_find_max_attr(client, data);
1658 pmbus_clear_fault_page(client, 0);
1659 return 0;
1662 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
1663 struct pmbus_driver_info *info)
1665 const struct pmbus_platform_data *pdata = client->dev.platform_data;
1666 struct pmbus_data *data;
1667 int ret;
1669 if (!info) {
1670 dev_err(&client->dev, "Missing chip information");
1671 return -ENODEV;
1674 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
1675 | I2C_FUNC_SMBUS_BYTE_DATA
1676 | I2C_FUNC_SMBUS_WORD_DATA))
1677 return -ENODEV;
1679 data = kzalloc(sizeof(*data), GFP_KERNEL);
1680 if (!data) {
1681 dev_err(&client->dev, "No memory to allocate driver data\n");
1682 return -ENOMEM;
1685 i2c_set_clientdata(client, data);
1686 mutex_init(&data->update_lock);
1688 /* Bail out if PMBus status register does not exist. */
1689 if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0) {
1690 dev_err(&client->dev, "PMBus status register not found\n");
1691 ret = -ENODEV;
1692 goto out_data;
1695 if (pdata)
1696 data->flags = pdata->flags;
1697 data->info = info;
1699 pmbus_clear_faults(client);
1701 if (info->identify) {
1702 ret = (*info->identify)(client, info);
1703 if (ret < 0) {
1704 dev_err(&client->dev, "Chip identification failed\n");
1705 goto out_data;
1709 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1710 dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
1711 info->pages);
1712 ret = -ENODEV;
1713 goto out_data;
1716 ret = pmbus_identify_common(client, data);
1717 if (ret < 0) {
1718 dev_err(&client->dev, "Failed to identify chip capabilities\n");
1719 goto out_data;
1722 ret = -ENOMEM;
1723 data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors,
1724 GFP_KERNEL);
1725 if (!data->sensors) {
1726 dev_err(&client->dev, "No memory to allocate sensor data\n");
1727 goto out_data;
1730 data->booleans = kzalloc(sizeof(struct pmbus_boolean)
1731 * data->max_booleans, GFP_KERNEL);
1732 if (!data->booleans) {
1733 dev_err(&client->dev, "No memory to allocate boolean data\n");
1734 goto out_sensors;
1737 data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels,
1738 GFP_KERNEL);
1739 if (!data->labels) {
1740 dev_err(&client->dev, "No memory to allocate label data\n");
1741 goto out_booleans;
1744 data->attributes = kzalloc(sizeof(struct attribute *)
1745 * data->max_attributes, GFP_KERNEL);
1746 if (!data->attributes) {
1747 dev_err(&client->dev, "No memory to allocate attribute data\n");
1748 goto out_labels;
1751 pmbus_find_attributes(client, data);
1754 * If there are no attributes, something is wrong.
1755 * Bail out instead of trying to register nothing.
1757 if (!data->num_attributes) {
1758 dev_err(&client->dev, "No attributes found\n");
1759 ret = -ENODEV;
1760 goto out_attributes;
1763 /* Register sysfs hooks */
1764 data->group.attrs = data->attributes;
1765 ret = sysfs_create_group(&client->dev.kobj, &data->group);
1766 if (ret) {
1767 dev_err(&client->dev, "Failed to create sysfs entries\n");
1768 goto out_attributes;
1770 data->hwmon_dev = hwmon_device_register(&client->dev);
1771 if (IS_ERR(data->hwmon_dev)) {
1772 ret = PTR_ERR(data->hwmon_dev);
1773 dev_err(&client->dev, "Failed to register hwmon device\n");
1774 goto out_hwmon_device_register;
1776 return 0;
1778 out_hwmon_device_register:
1779 sysfs_remove_group(&client->dev.kobj, &data->group);
1780 out_attributes:
1781 kfree(data->attributes);
1782 out_labels:
1783 kfree(data->labels);
1784 out_booleans:
1785 kfree(data->booleans);
1786 out_sensors:
1787 kfree(data->sensors);
1788 out_data:
1789 kfree(data);
1790 return ret;
1792 EXPORT_SYMBOL_GPL(pmbus_do_probe);
1794 void pmbus_do_remove(struct i2c_client *client)
1796 struct pmbus_data *data = i2c_get_clientdata(client);
1797 hwmon_device_unregister(data->hwmon_dev);
1798 sysfs_remove_group(&client->dev.kobj, &data->group);
1799 kfree(data->attributes);
1800 kfree(data->labels);
1801 kfree(data->booleans);
1802 kfree(data->sensors);
1803 kfree(data);
1805 EXPORT_SYMBOL_GPL(pmbus_do_remove);
1807 MODULE_AUTHOR("Guenter Roeck");
1808 MODULE_DESCRIPTION("PMBus core driver");
1809 MODULE_LICENSE("GPL");