Linux 3.3-rc6
[linux/fpc-iii.git] / drivers / hwmon / pmbus / pmbus_core.c
blob00460d8d84239e57c880238469df0f2965c9b976
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 2 /* alarm, crit_alarm */
58 #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
59 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
60 lcrit_alarm, crit_alarm */
62 #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
65 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
66 * are paged. status_input is unpaged.
68 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
71 * Index into status register array, per status register group
73 #define PB_STATUS_BASE 0
74 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
75 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
76 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
77 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
78 #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
79 #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
81 #define PMBUS_NAME_SIZE 24
83 struct pmbus_sensor {
84 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */
85 struct sensor_device_attribute attribute;
86 u8 page; /* page number */
87 u16 reg; /* register */
88 enum pmbus_sensor_classes class; /* sensor class */
89 bool update; /* runtime sensor update needed */
90 int data; /* Sensor data.
91 Negative if there was a read error */
94 struct pmbus_boolean {
95 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */
96 struct sensor_device_attribute attribute;
99 struct pmbus_label {
100 char name[PMBUS_NAME_SIZE]; /* sysfs label name */
101 struct sensor_device_attribute attribute;
102 char label[PMBUS_NAME_SIZE]; /* label */
105 struct pmbus_data {
106 struct device *hwmon_dev;
108 u32 flags; /* from platform data */
110 int exponent; /* linear mode: exponent for output voltages */
112 const struct pmbus_driver_info *info;
114 int max_attributes;
115 int num_attributes;
116 struct attribute **attributes;
117 struct attribute_group group;
120 * Sensors cover both sensor and limit registers.
122 int max_sensors;
123 int num_sensors;
124 struct pmbus_sensor *sensors;
126 * Booleans are used for alarms.
127 * Values are determined from status registers.
129 int max_booleans;
130 int num_booleans;
131 struct pmbus_boolean *booleans;
133 * Labels are used to map generic names (e.g., "in1")
134 * to PMBus specific names (e.g., "vin" or "vout1").
136 int max_labels;
137 int num_labels;
138 struct pmbus_label *labels;
140 struct mutex update_lock;
141 bool valid;
142 unsigned long last_updated; /* in jiffies */
145 * A single status register covers multiple attributes,
146 * so we keep them all together.
148 u8 status[PB_NUM_STATUS_REG];
150 u8 currpage;
153 int pmbus_set_page(struct i2c_client *client, u8 page)
155 struct pmbus_data *data = i2c_get_clientdata(client);
156 int rv = 0;
157 int newpage;
159 if (page != data->currpage) {
160 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
161 newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
162 if (newpage != page)
163 rv = -EIO;
164 else
165 data->currpage = page;
167 return rv;
169 EXPORT_SYMBOL_GPL(pmbus_set_page);
171 int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
173 int rv;
175 if (page >= 0) {
176 rv = pmbus_set_page(client, page);
177 if (rv < 0)
178 return rv;
181 return i2c_smbus_write_byte(client, value);
183 EXPORT_SYMBOL_GPL(pmbus_write_byte);
186 * _pmbus_write_byte() is similar to pmbus_write_byte(), but checks if
187 * a device specific mapping funcion exists and calls it if necessary.
189 static int _pmbus_write_byte(struct i2c_client *client, int page, u8 value)
191 struct pmbus_data *data = i2c_get_clientdata(client);
192 const struct pmbus_driver_info *info = data->info;
193 int status;
195 if (info->write_byte) {
196 status = info->write_byte(client, page, value);
197 if (status != -ENODATA)
198 return status;
200 return pmbus_write_byte(client, page, value);
203 int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word)
205 int rv;
207 rv = pmbus_set_page(client, page);
208 if (rv < 0)
209 return rv;
211 return i2c_smbus_write_word_data(client, reg, word);
213 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
216 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
217 * a device specific mapping function exists and calls it if necessary.
219 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
220 u16 word)
222 struct pmbus_data *data = i2c_get_clientdata(client);
223 const struct pmbus_driver_info *info = data->info;
224 int status;
226 if (info->write_word_data) {
227 status = info->write_word_data(client, page, reg, word);
228 if (status != -ENODATA)
229 return status;
231 if (reg >= PMBUS_VIRT_BASE)
232 return -ENXIO;
233 return pmbus_write_word_data(client, page, reg, word);
236 int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
238 int rv;
240 rv = pmbus_set_page(client, page);
241 if (rv < 0)
242 return rv;
244 return i2c_smbus_read_word_data(client, reg);
246 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
249 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
250 * a device specific mapping function exists and calls it if necessary.
252 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
254 struct pmbus_data *data = i2c_get_clientdata(client);
255 const struct pmbus_driver_info *info = data->info;
256 int status;
258 if (info->read_word_data) {
259 status = info->read_word_data(client, page, reg);
260 if (status != -ENODATA)
261 return status;
263 if (reg >= PMBUS_VIRT_BASE)
264 return -ENXIO;
265 return pmbus_read_word_data(client, page, reg);
268 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
270 int rv;
272 if (page >= 0) {
273 rv = pmbus_set_page(client, page);
274 if (rv < 0)
275 return rv;
278 return i2c_smbus_read_byte_data(client, reg);
280 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
283 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
284 * a device specific mapping function exists and calls it if necessary.
286 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
288 struct pmbus_data *data = i2c_get_clientdata(client);
289 const struct pmbus_driver_info *info = data->info;
290 int status;
292 if (info->read_byte_data) {
293 status = info->read_byte_data(client, page, reg);
294 if (status != -ENODATA)
295 return status;
297 return pmbus_read_byte_data(client, page, reg);
300 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
302 _pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
305 void pmbus_clear_faults(struct i2c_client *client)
307 struct pmbus_data *data = i2c_get_clientdata(client);
308 int i;
310 for (i = 0; i < data->info->pages; i++)
311 pmbus_clear_fault_page(client, i);
313 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
315 static int pmbus_check_status_cml(struct i2c_client *client)
317 int status, status2;
319 status = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_BYTE);
320 if (status < 0 || (status & PB_STATUS_CML)) {
321 status2 = _pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
322 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
323 return -EIO;
325 return 0;
328 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
330 int rv;
331 struct pmbus_data *data = i2c_get_clientdata(client);
333 rv = _pmbus_read_byte_data(client, page, reg);
334 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
335 rv = pmbus_check_status_cml(client);
336 pmbus_clear_fault_page(client, -1);
337 return rv >= 0;
339 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
341 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
343 int rv;
344 struct pmbus_data *data = i2c_get_clientdata(client);
346 rv = _pmbus_read_word_data(client, page, reg);
347 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
348 rv = pmbus_check_status_cml(client);
349 pmbus_clear_fault_page(client, -1);
350 return rv >= 0;
352 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
354 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
356 struct pmbus_data *data = i2c_get_clientdata(client);
358 return data->info;
360 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
362 static struct pmbus_data *pmbus_update_device(struct device *dev)
364 struct i2c_client *client = to_i2c_client(dev);
365 struct pmbus_data *data = i2c_get_clientdata(client);
366 const struct pmbus_driver_info *info = data->info;
368 mutex_lock(&data->update_lock);
369 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
370 int i;
372 for (i = 0; i < info->pages; i++)
373 data->status[PB_STATUS_BASE + i]
374 = _pmbus_read_byte_data(client, i,
375 PMBUS_STATUS_BYTE);
376 for (i = 0; i < info->pages; i++) {
377 if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT))
378 continue;
379 data->status[PB_STATUS_VOUT_BASE + i]
380 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_VOUT);
382 for (i = 0; i < info->pages; i++) {
383 if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT))
384 continue;
385 data->status[PB_STATUS_IOUT_BASE + i]
386 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_IOUT);
388 for (i = 0; i < info->pages; i++) {
389 if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP))
390 continue;
391 data->status[PB_STATUS_TEMP_BASE + i]
392 = _pmbus_read_byte_data(client, i,
393 PMBUS_STATUS_TEMPERATURE);
395 for (i = 0; i < info->pages; i++) {
396 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12))
397 continue;
398 data->status[PB_STATUS_FAN_BASE + i]
399 = _pmbus_read_byte_data(client, i,
400 PMBUS_STATUS_FAN_12);
403 for (i = 0; i < info->pages; i++) {
404 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34))
405 continue;
406 data->status[PB_STATUS_FAN34_BASE + i]
407 = _pmbus_read_byte_data(client, i,
408 PMBUS_STATUS_FAN_34);
411 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
412 data->status[PB_STATUS_INPUT_BASE]
413 = _pmbus_read_byte_data(client, 0,
414 PMBUS_STATUS_INPUT);
416 for (i = 0; i < data->num_sensors; i++) {
417 struct pmbus_sensor *sensor = &data->sensors[i];
419 if (!data->valid || sensor->update)
420 sensor->data
421 = _pmbus_read_word_data(client,
422 sensor->page,
423 sensor->reg);
425 pmbus_clear_faults(client);
426 data->last_updated = jiffies;
427 data->valid = 1;
429 mutex_unlock(&data->update_lock);
430 return data;
434 * Convert linear sensor values to milli- or micro-units
435 * depending on sensor type.
437 static long pmbus_reg2data_linear(struct pmbus_data *data,
438 struct pmbus_sensor *sensor)
440 s16 exponent;
441 s32 mantissa;
442 long val;
444 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
445 exponent = data->exponent;
446 mantissa = (u16) sensor->data;
447 } else { /* LINEAR11 */
448 exponent = ((s16)sensor->data) >> 11;
449 mantissa = ((s16)((sensor->data & 0x7ff) << 5)) >> 5;
452 val = mantissa;
454 /* scale result to milli-units for all sensors except fans */
455 if (sensor->class != PSC_FAN)
456 val = val * 1000L;
458 /* scale result to micro-units for power sensors */
459 if (sensor->class == PSC_POWER)
460 val = val * 1000L;
462 if (exponent >= 0)
463 val <<= exponent;
464 else
465 val >>= -exponent;
467 return val;
471 * Convert direct sensor values to milli- or micro-units
472 * depending on sensor type.
474 static long pmbus_reg2data_direct(struct pmbus_data *data,
475 struct pmbus_sensor *sensor)
477 long val = (s16) sensor->data;
478 long m, b, R;
480 m = data->info->m[sensor->class];
481 b = data->info->b[sensor->class];
482 R = data->info->R[sensor->class];
484 if (m == 0)
485 return 0;
487 /* X = 1/m * (Y * 10^-R - b) */
488 R = -R;
489 /* scale result to milli-units for everything but fans */
490 if (sensor->class != PSC_FAN) {
491 R += 3;
492 b *= 1000;
495 /* scale result to micro-units for power sensors */
496 if (sensor->class == PSC_POWER) {
497 R += 3;
498 b *= 1000;
501 while (R > 0) {
502 val *= 10;
503 R--;
505 while (R < 0) {
506 val = DIV_ROUND_CLOSEST(val, 10);
507 R++;
510 return (val - b) / m;
514 * Convert VID sensor values to milli- or micro-units
515 * depending on sensor type.
516 * We currently only support VR11.
518 static long pmbus_reg2data_vid(struct pmbus_data *data,
519 struct pmbus_sensor *sensor)
521 long val = sensor->data;
523 if (val < 0x02 || val > 0xb2)
524 return 0;
525 return DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
528 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
530 long val;
532 switch (data->info->format[sensor->class]) {
533 case direct:
534 val = pmbus_reg2data_direct(data, sensor);
535 break;
536 case vid:
537 val = pmbus_reg2data_vid(data, sensor);
538 break;
539 case linear:
540 default:
541 val = pmbus_reg2data_linear(data, sensor);
542 break;
544 return val;
547 #define MAX_MANTISSA (1023 * 1000)
548 #define MIN_MANTISSA (511 * 1000)
550 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
551 enum pmbus_sensor_classes class, long val)
553 s16 exponent = 0, mantissa;
554 bool negative = false;
556 /* simple case */
557 if (val == 0)
558 return 0;
560 if (class == PSC_VOLTAGE_OUT) {
561 /* LINEAR16 does not support negative voltages */
562 if (val < 0)
563 return 0;
566 * For a static exponents, we don't have a choice
567 * but to adjust the value to it.
569 if (data->exponent < 0)
570 val <<= -data->exponent;
571 else
572 val >>= data->exponent;
573 val = DIV_ROUND_CLOSEST(val, 1000);
574 return val & 0xffff;
577 if (val < 0) {
578 negative = true;
579 val = -val;
582 /* Power is in uW. Convert to mW before converting. */
583 if (class == PSC_POWER)
584 val = DIV_ROUND_CLOSEST(val, 1000L);
587 * For simplicity, convert fan data to milli-units
588 * before calculating the exponent.
590 if (class == PSC_FAN)
591 val = val * 1000;
593 /* Reduce large mantissa until it fits into 10 bit */
594 while (val >= MAX_MANTISSA && exponent < 15) {
595 exponent++;
596 val >>= 1;
598 /* Increase small mantissa to improve precision */
599 while (val < MIN_MANTISSA && exponent > -15) {
600 exponent--;
601 val <<= 1;
604 /* Convert mantissa from milli-units to units */
605 mantissa = DIV_ROUND_CLOSEST(val, 1000);
607 /* Ensure that resulting number is within range */
608 if (mantissa > 0x3ff)
609 mantissa = 0x3ff;
611 /* restore sign */
612 if (negative)
613 mantissa = -mantissa;
615 /* Convert to 5 bit exponent, 11 bit mantissa */
616 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
619 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
620 enum pmbus_sensor_classes class, long val)
622 long m, b, R;
624 m = data->info->m[class];
625 b = data->info->b[class];
626 R = data->info->R[class];
628 /* Power is in uW. Adjust R and b. */
629 if (class == PSC_POWER) {
630 R -= 3;
631 b *= 1000;
634 /* Calculate Y = (m * X + b) * 10^R */
635 if (class != PSC_FAN) {
636 R -= 3; /* Adjust R and b for data in milli-units */
637 b *= 1000;
639 val = val * m + b;
641 while (R > 0) {
642 val *= 10;
643 R--;
645 while (R < 0) {
646 val = DIV_ROUND_CLOSEST(val, 10);
647 R++;
650 return val;
653 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
654 enum pmbus_sensor_classes class, long val)
656 val = SENSORS_LIMIT(val, 500, 1600);
658 return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
661 static u16 pmbus_data2reg(struct pmbus_data *data,
662 enum pmbus_sensor_classes class, long val)
664 u16 regval;
666 switch (data->info->format[class]) {
667 case direct:
668 regval = pmbus_data2reg_direct(data, class, val);
669 break;
670 case vid:
671 regval = pmbus_data2reg_vid(data, class, val);
672 break;
673 case linear:
674 default:
675 regval = pmbus_data2reg_linear(data, class, val);
676 break;
678 return regval;
682 * Return boolean calculated from converted data.
683 * <index> defines a status register index and mask, and optionally
684 * two sensor indexes.
685 * The upper half-word references the two sensors,
686 * two sensor indices.
687 * The upper half-word references the two optional sensors,
688 * the lower half word references status register and mask.
689 * The function returns true if (status[reg] & mask) is true and,
690 * if specified, if v1 >= v2.
691 * To determine if an object exceeds upper limits, specify <v, limit>.
692 * To determine if an object exceeds lower limits, specify <limit, v>.
694 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
695 * index are set. s1 and s2 (the sensor index values) are zero in this case.
696 * The function returns true if (status[reg] & mask) is true.
698 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
699 * a specified limit has to be performed to determine the boolean result.
700 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
701 * sensor values referenced by sensor indices s1 and s2).
703 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
704 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
706 * If a negative value is stored in any of the referenced registers, this value
707 * reflects an error code which will be returned.
709 static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val)
711 u8 s1 = (index >> 24) & 0xff;
712 u8 s2 = (index >> 16) & 0xff;
713 u8 reg = (index >> 8) & 0xff;
714 u8 mask = index & 0xff;
715 int status;
716 u8 regval;
718 status = data->status[reg];
719 if (status < 0)
720 return status;
722 regval = status & mask;
723 if (!s1 && !s2)
724 *val = !!regval;
725 else {
726 long v1, v2;
727 struct pmbus_sensor *sensor1, *sensor2;
729 sensor1 = &data->sensors[s1];
730 if (sensor1->data < 0)
731 return sensor1->data;
732 sensor2 = &data->sensors[s2];
733 if (sensor2->data < 0)
734 return sensor2->data;
736 v1 = pmbus_reg2data(data, sensor1);
737 v2 = pmbus_reg2data(data, sensor2);
738 *val = !!(regval && v1 >= v2);
740 return 0;
743 static ssize_t pmbus_show_boolean(struct device *dev,
744 struct device_attribute *da, char *buf)
746 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
747 struct pmbus_data *data = pmbus_update_device(dev);
748 int val;
749 int err;
751 err = pmbus_get_boolean(data, attr->index, &val);
752 if (err)
753 return err;
754 return snprintf(buf, PAGE_SIZE, "%d\n", val);
757 static ssize_t pmbus_show_sensor(struct device *dev,
758 struct device_attribute *da, char *buf)
760 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
761 struct pmbus_data *data = pmbus_update_device(dev);
762 struct pmbus_sensor *sensor;
764 sensor = &data->sensors[attr->index];
765 if (sensor->data < 0)
766 return sensor->data;
768 return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
771 static ssize_t pmbus_set_sensor(struct device *dev,
772 struct device_attribute *devattr,
773 const char *buf, size_t count)
775 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
776 struct i2c_client *client = to_i2c_client(dev);
777 struct pmbus_data *data = i2c_get_clientdata(client);
778 struct pmbus_sensor *sensor = &data->sensors[attr->index];
779 ssize_t rv = count;
780 long val = 0;
781 int ret;
782 u16 regval;
784 if (strict_strtol(buf, 10, &val) < 0)
785 return -EINVAL;
787 mutex_lock(&data->update_lock);
788 regval = pmbus_data2reg(data, sensor->class, val);
789 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
790 if (ret < 0)
791 rv = ret;
792 else
793 data->sensors[attr->index].data = regval;
794 mutex_unlock(&data->update_lock);
795 return rv;
798 static ssize_t pmbus_show_label(struct device *dev,
799 struct device_attribute *da, char *buf)
801 struct i2c_client *client = to_i2c_client(dev);
802 struct pmbus_data *data = i2c_get_clientdata(client);
803 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
805 return snprintf(buf, PAGE_SIZE, "%s\n",
806 data->labels[attr->index].label);
809 #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
810 do { \
811 struct sensor_device_attribute *a \
812 = &data->_type##s[data->num_##_type##s].attribute; \
813 BUG_ON(data->num_attributes >= data->max_attributes); \
814 sysfs_attr_init(&a->dev_attr.attr); \
815 a->dev_attr.attr.name = _name; \
816 a->dev_attr.attr.mode = _mode; \
817 a->dev_attr.show = _show; \
818 a->dev_attr.store = _set; \
819 a->index = _idx; \
820 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
821 data->num_attributes++; \
822 } while (0)
824 #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
825 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
826 pmbus_show_##_type, NULL)
828 #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
829 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
830 pmbus_show_##_type, pmbus_set_##_type)
832 static void pmbus_add_boolean(struct pmbus_data *data,
833 const char *name, const char *type, int seq,
834 int idx)
836 struct pmbus_boolean *boolean;
838 BUG_ON(data->num_booleans >= data->max_booleans);
840 boolean = &data->booleans[data->num_booleans];
842 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
843 name, seq, type);
844 PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx);
845 data->num_booleans++;
848 static void pmbus_add_boolean_reg(struct pmbus_data *data,
849 const char *name, const char *type,
850 int seq, int reg, int bit)
852 pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit);
855 static void pmbus_add_boolean_cmp(struct pmbus_data *data,
856 const char *name, const char *type,
857 int seq, int i1, int i2, int reg, int mask)
859 pmbus_add_boolean(data, name, type, seq,
860 (i1 << 24) | (i2 << 16) | (reg << 8) | mask);
863 static void pmbus_add_sensor(struct pmbus_data *data,
864 const char *name, const char *type, int seq,
865 int page, int reg, enum pmbus_sensor_classes class,
866 bool update, bool readonly)
868 struct pmbus_sensor *sensor;
870 BUG_ON(data->num_sensors >= data->max_sensors);
872 sensor = &data->sensors[data->num_sensors];
873 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
874 name, seq, type);
875 sensor->page = page;
876 sensor->reg = reg;
877 sensor->class = class;
878 sensor->update = update;
879 if (readonly)
880 PMBUS_ADD_GET_ATTR(data, sensor->name, sensor,
881 data->num_sensors);
882 else
883 PMBUS_ADD_SET_ATTR(data, sensor->name, sensor,
884 data->num_sensors);
885 data->num_sensors++;
888 static void pmbus_add_label(struct pmbus_data *data,
889 const char *name, int seq,
890 const char *lstring, int index)
892 struct pmbus_label *label;
894 BUG_ON(data->num_labels >= data->max_labels);
896 label = &data->labels[data->num_labels];
897 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
898 if (!index)
899 strncpy(label->label, lstring, sizeof(label->label) - 1);
900 else
901 snprintf(label->label, sizeof(label->label), "%s%d", lstring,
902 index);
904 PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels);
905 data->num_labels++;
909 * Determine maximum number of sensors, booleans, and labels.
910 * To keep things simple, only make a rough high estimate.
912 static void pmbus_find_max_attr(struct i2c_client *client,
913 struct pmbus_data *data)
915 const struct pmbus_driver_info *info = data->info;
916 int page, max_sensors, max_booleans, max_labels;
918 max_sensors = PMBUS_MAX_INPUT_SENSORS;
919 max_booleans = PMBUS_MAX_INPUT_BOOLEANS;
920 max_labels = PMBUS_MAX_INPUT_LABELS;
922 for (page = 0; page < info->pages; page++) {
923 if (info->func[page] & PMBUS_HAVE_VOUT) {
924 max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE;
925 max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE;
926 max_labels++;
928 if (info->func[page] & PMBUS_HAVE_IOUT) {
929 max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE;
930 max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE;
931 max_labels++;
933 if (info->func[page] & PMBUS_HAVE_POUT) {
934 max_sensors += PMBUS_POUT_SENSORS_PER_PAGE;
935 max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE;
936 max_labels++;
938 if (info->func[page] & PMBUS_HAVE_FAN12) {
939 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
940 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
942 if (info->func[page] & PMBUS_HAVE_FAN34) {
943 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
944 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
946 if (info->func[page] & PMBUS_HAVE_TEMP) {
947 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
948 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
950 if (info->func[page] & PMBUS_HAVE_TEMP2) {
951 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
952 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
954 if (info->func[page] & PMBUS_HAVE_TEMP3) {
955 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
956 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
959 data->max_sensors = max_sensors;
960 data->max_booleans = max_booleans;
961 data->max_labels = max_labels;
962 data->max_attributes = max_sensors + max_booleans + max_labels;
966 * Search for attributes. Allocate sensors, booleans, and labels as needed.
970 * The pmbus_limit_attr structure describes a single limit attribute
971 * and its associated alarm attribute.
973 struct pmbus_limit_attr {
974 u16 reg; /* Limit register */
975 bool update; /* True if register needs updates */
976 bool low; /* True if low limit; for limits with compare
977 functions only */
978 const char *attr; /* Attribute name */
979 const char *alarm; /* Alarm attribute name */
980 u32 sbit; /* Alarm attribute status bit */
984 * The pmbus_sensor_attr structure describes one sensor attribute. This
985 * description includes a reference to the associated limit attributes.
987 struct pmbus_sensor_attr {
988 u8 reg; /* sensor register */
989 enum pmbus_sensor_classes class;/* sensor class */
990 const char *label; /* sensor label */
991 bool paged; /* true if paged sensor */
992 bool update; /* true if update needed */
993 bool compare; /* true if compare function needed */
994 u32 func; /* sensor mask */
995 u32 sfunc; /* sensor status mask */
996 int sbase; /* status base register */
997 u32 gbit; /* generic status bit */
998 const struct pmbus_limit_attr *limit;/* limit registers */
999 int nlimit; /* # of limit registers */
1003 * Add a set of limit attributes and, if supported, the associated
1004 * alarm attributes.
1006 static bool pmbus_add_limit_attrs(struct i2c_client *client,
1007 struct pmbus_data *data,
1008 const struct pmbus_driver_info *info,
1009 const char *name, int index, int page,
1010 int cbase,
1011 const struct pmbus_sensor_attr *attr)
1013 const struct pmbus_limit_attr *l = attr->limit;
1014 int nlimit = attr->nlimit;
1015 bool have_alarm = false;
1016 int i, cindex;
1018 for (i = 0; i < nlimit; i++) {
1019 if (pmbus_check_word_register(client, page, l->reg)) {
1020 cindex = data->num_sensors;
1021 pmbus_add_sensor(data, name, l->attr, index, page,
1022 l->reg, attr->class,
1023 attr->update || l->update,
1024 false);
1025 if (l->sbit && (info->func[page] & attr->sfunc)) {
1026 if (attr->compare) {
1027 pmbus_add_boolean_cmp(data, name,
1028 l->alarm, index,
1029 l->low ? cindex : cbase,
1030 l->low ? cbase : cindex,
1031 attr->sbase + page, l->sbit);
1032 } else {
1033 pmbus_add_boolean_reg(data, name,
1034 l->alarm, index,
1035 attr->sbase + page, l->sbit);
1037 have_alarm = true;
1040 l++;
1042 return have_alarm;
1045 static void pmbus_add_sensor_attrs_one(struct i2c_client *client,
1046 struct pmbus_data *data,
1047 const struct pmbus_driver_info *info,
1048 const char *name,
1049 int index, int page,
1050 const struct pmbus_sensor_attr *attr)
1052 bool have_alarm;
1053 int cbase = data->num_sensors;
1055 if (attr->label)
1056 pmbus_add_label(data, name, index, attr->label,
1057 attr->paged ? page + 1 : 0);
1058 pmbus_add_sensor(data, name, "input", index, page, attr->reg,
1059 attr->class, true, true);
1060 if (attr->sfunc) {
1061 have_alarm = pmbus_add_limit_attrs(client, data, info, name,
1062 index, page, cbase, attr);
1064 * Add generic alarm attribute only if there are no individual
1065 * alarm attributes, if there is a global alarm bit, and if
1066 * the generic status register for this page is accessible.
1068 if (!have_alarm && attr->gbit &&
1069 pmbus_check_byte_register(client, page, PMBUS_STATUS_BYTE))
1070 pmbus_add_boolean_reg(data, name, "alarm", index,
1071 PB_STATUS_BASE + page,
1072 attr->gbit);
1076 static void pmbus_add_sensor_attrs(struct i2c_client *client,
1077 struct pmbus_data *data,
1078 const char *name,
1079 const struct pmbus_sensor_attr *attrs,
1080 int nattrs)
1082 const struct pmbus_driver_info *info = data->info;
1083 int index, i;
1085 index = 1;
1086 for (i = 0; i < nattrs; i++) {
1087 int page, pages;
1089 pages = attrs->paged ? info->pages : 1;
1090 for (page = 0; page < pages; page++) {
1091 if (!(info->func[page] & attrs->func))
1092 continue;
1093 pmbus_add_sensor_attrs_one(client, data, info, name,
1094 index, page, attrs);
1095 index++;
1097 attrs++;
1101 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1103 .reg = PMBUS_VIN_UV_WARN_LIMIT,
1104 .attr = "min",
1105 .alarm = "min_alarm",
1106 .sbit = PB_VOLTAGE_UV_WARNING,
1107 }, {
1108 .reg = PMBUS_VIN_UV_FAULT_LIMIT,
1109 .attr = "lcrit",
1110 .alarm = "lcrit_alarm",
1111 .sbit = PB_VOLTAGE_UV_FAULT,
1112 }, {
1113 .reg = PMBUS_VIN_OV_WARN_LIMIT,
1114 .attr = "max",
1115 .alarm = "max_alarm",
1116 .sbit = PB_VOLTAGE_OV_WARNING,
1117 }, {
1118 .reg = PMBUS_VIN_OV_FAULT_LIMIT,
1119 .attr = "crit",
1120 .alarm = "crit_alarm",
1121 .sbit = PB_VOLTAGE_OV_FAULT,
1122 }, {
1123 .reg = PMBUS_VIRT_READ_VIN_AVG,
1124 .update = true,
1125 .attr = "average",
1126 }, {
1127 .reg = PMBUS_VIRT_READ_VIN_MIN,
1128 .update = true,
1129 .attr = "lowest",
1130 }, {
1131 .reg = PMBUS_VIRT_READ_VIN_MAX,
1132 .update = true,
1133 .attr = "highest",
1134 }, {
1135 .reg = PMBUS_VIRT_RESET_VIN_HISTORY,
1136 .attr = "reset_history",
1140 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1142 .reg = PMBUS_VOUT_UV_WARN_LIMIT,
1143 .attr = "min",
1144 .alarm = "min_alarm",
1145 .sbit = PB_VOLTAGE_UV_WARNING,
1146 }, {
1147 .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1148 .attr = "lcrit",
1149 .alarm = "lcrit_alarm",
1150 .sbit = PB_VOLTAGE_UV_FAULT,
1151 }, {
1152 .reg = PMBUS_VOUT_OV_WARN_LIMIT,
1153 .attr = "max",
1154 .alarm = "max_alarm",
1155 .sbit = PB_VOLTAGE_OV_WARNING,
1156 }, {
1157 .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1158 .attr = "crit",
1159 .alarm = "crit_alarm",
1160 .sbit = PB_VOLTAGE_OV_FAULT,
1161 }, {
1162 .reg = PMBUS_VIRT_READ_VOUT_AVG,
1163 .update = true,
1164 .attr = "average",
1165 }, {
1166 .reg = PMBUS_VIRT_READ_VOUT_MIN,
1167 .update = true,
1168 .attr = "lowest",
1169 }, {
1170 .reg = PMBUS_VIRT_READ_VOUT_MAX,
1171 .update = true,
1172 .attr = "highest",
1173 }, {
1174 .reg = PMBUS_VIRT_RESET_VOUT_HISTORY,
1175 .attr = "reset_history",
1179 static const struct pmbus_sensor_attr voltage_attributes[] = {
1181 .reg = PMBUS_READ_VIN,
1182 .class = PSC_VOLTAGE_IN,
1183 .label = "vin",
1184 .func = PMBUS_HAVE_VIN,
1185 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1186 .sbase = PB_STATUS_INPUT_BASE,
1187 .gbit = PB_STATUS_VIN_UV,
1188 .limit = vin_limit_attrs,
1189 .nlimit = ARRAY_SIZE(vin_limit_attrs),
1190 }, {
1191 .reg = PMBUS_READ_VCAP,
1192 .class = PSC_VOLTAGE_IN,
1193 .label = "vcap",
1194 .func = PMBUS_HAVE_VCAP,
1195 }, {
1196 .reg = PMBUS_READ_VOUT,
1197 .class = PSC_VOLTAGE_OUT,
1198 .label = "vout",
1199 .paged = true,
1200 .func = PMBUS_HAVE_VOUT,
1201 .sfunc = PMBUS_HAVE_STATUS_VOUT,
1202 .sbase = PB_STATUS_VOUT_BASE,
1203 .gbit = PB_STATUS_VOUT_OV,
1204 .limit = vout_limit_attrs,
1205 .nlimit = ARRAY_SIZE(vout_limit_attrs),
1209 /* Current attributes */
1211 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1213 .reg = PMBUS_IIN_OC_WARN_LIMIT,
1214 .attr = "max",
1215 .alarm = "max_alarm",
1216 .sbit = PB_IIN_OC_WARNING,
1217 }, {
1218 .reg = PMBUS_IIN_OC_FAULT_LIMIT,
1219 .attr = "crit",
1220 .alarm = "crit_alarm",
1221 .sbit = PB_IIN_OC_FAULT,
1222 }, {
1223 .reg = PMBUS_VIRT_READ_IIN_AVG,
1224 .update = true,
1225 .attr = "average",
1226 }, {
1227 .reg = PMBUS_VIRT_READ_IIN_MIN,
1228 .update = true,
1229 .attr = "lowest",
1230 }, {
1231 .reg = PMBUS_VIRT_READ_IIN_MAX,
1232 .update = true,
1233 .attr = "highest",
1234 }, {
1235 .reg = PMBUS_VIRT_RESET_IIN_HISTORY,
1236 .attr = "reset_history",
1240 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1242 .reg = PMBUS_IOUT_OC_WARN_LIMIT,
1243 .attr = "max",
1244 .alarm = "max_alarm",
1245 .sbit = PB_IOUT_OC_WARNING,
1246 }, {
1247 .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1248 .attr = "lcrit",
1249 .alarm = "lcrit_alarm",
1250 .sbit = PB_IOUT_UC_FAULT,
1251 }, {
1252 .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1253 .attr = "crit",
1254 .alarm = "crit_alarm",
1255 .sbit = PB_IOUT_OC_FAULT,
1256 }, {
1257 .reg = PMBUS_VIRT_READ_IOUT_AVG,
1258 .update = true,
1259 .attr = "average",
1260 }, {
1261 .reg = PMBUS_VIRT_READ_IOUT_MIN,
1262 .update = true,
1263 .attr = "lowest",
1264 }, {
1265 .reg = PMBUS_VIRT_READ_IOUT_MAX,
1266 .update = true,
1267 .attr = "highest",
1268 }, {
1269 .reg = PMBUS_VIRT_RESET_IOUT_HISTORY,
1270 .attr = "reset_history",
1274 static const struct pmbus_sensor_attr current_attributes[] = {
1276 .reg = PMBUS_READ_IIN,
1277 .class = PSC_CURRENT_IN,
1278 .label = "iin",
1279 .func = PMBUS_HAVE_IIN,
1280 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1281 .sbase = PB_STATUS_INPUT_BASE,
1282 .limit = iin_limit_attrs,
1283 .nlimit = ARRAY_SIZE(iin_limit_attrs),
1284 }, {
1285 .reg = PMBUS_READ_IOUT,
1286 .class = PSC_CURRENT_OUT,
1287 .label = "iout",
1288 .paged = true,
1289 .func = PMBUS_HAVE_IOUT,
1290 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1291 .sbase = PB_STATUS_IOUT_BASE,
1292 .gbit = PB_STATUS_IOUT_OC,
1293 .limit = iout_limit_attrs,
1294 .nlimit = ARRAY_SIZE(iout_limit_attrs),
1298 /* Power attributes */
1300 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1302 .reg = PMBUS_PIN_OP_WARN_LIMIT,
1303 .attr = "max",
1304 .alarm = "alarm",
1305 .sbit = PB_PIN_OP_WARNING,
1306 }, {
1307 .reg = PMBUS_VIRT_READ_PIN_AVG,
1308 .update = true,
1309 .attr = "average",
1310 }, {
1311 .reg = PMBUS_VIRT_READ_PIN_MAX,
1312 .update = true,
1313 .attr = "input_highest",
1314 }, {
1315 .reg = PMBUS_VIRT_RESET_PIN_HISTORY,
1316 .attr = "reset_history",
1320 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1322 .reg = PMBUS_POUT_MAX,
1323 .attr = "cap",
1324 .alarm = "cap_alarm",
1325 .sbit = PB_POWER_LIMITING,
1326 }, {
1327 .reg = PMBUS_POUT_OP_WARN_LIMIT,
1328 .attr = "max",
1329 .alarm = "max_alarm",
1330 .sbit = PB_POUT_OP_WARNING,
1331 }, {
1332 .reg = PMBUS_POUT_OP_FAULT_LIMIT,
1333 .attr = "crit",
1334 .alarm = "crit_alarm",
1335 .sbit = PB_POUT_OP_FAULT,
1339 static const struct pmbus_sensor_attr power_attributes[] = {
1341 .reg = PMBUS_READ_PIN,
1342 .class = PSC_POWER,
1343 .label = "pin",
1344 .func = PMBUS_HAVE_PIN,
1345 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1346 .sbase = PB_STATUS_INPUT_BASE,
1347 .limit = pin_limit_attrs,
1348 .nlimit = ARRAY_SIZE(pin_limit_attrs),
1349 }, {
1350 .reg = PMBUS_READ_POUT,
1351 .class = PSC_POWER,
1352 .label = "pout",
1353 .paged = true,
1354 .func = PMBUS_HAVE_POUT,
1355 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1356 .sbase = PB_STATUS_IOUT_BASE,
1357 .limit = pout_limit_attrs,
1358 .nlimit = ARRAY_SIZE(pout_limit_attrs),
1362 /* Temperature atributes */
1364 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1366 .reg = PMBUS_UT_WARN_LIMIT,
1367 .low = true,
1368 .attr = "min",
1369 .alarm = "min_alarm",
1370 .sbit = PB_TEMP_UT_WARNING,
1371 }, {
1372 .reg = PMBUS_UT_FAULT_LIMIT,
1373 .low = true,
1374 .attr = "lcrit",
1375 .alarm = "lcrit_alarm",
1376 .sbit = PB_TEMP_UT_FAULT,
1377 }, {
1378 .reg = PMBUS_OT_WARN_LIMIT,
1379 .attr = "max",
1380 .alarm = "max_alarm",
1381 .sbit = PB_TEMP_OT_WARNING,
1382 }, {
1383 .reg = PMBUS_OT_FAULT_LIMIT,
1384 .attr = "crit",
1385 .alarm = "crit_alarm",
1386 .sbit = PB_TEMP_OT_FAULT,
1387 }, {
1388 .reg = PMBUS_VIRT_READ_TEMP_MIN,
1389 .attr = "lowest",
1390 }, {
1391 .reg = PMBUS_VIRT_READ_TEMP_MAX,
1392 .attr = "highest",
1393 }, {
1394 .reg = PMBUS_VIRT_RESET_TEMP_HISTORY,
1395 .attr = "reset_history",
1399 static const struct pmbus_limit_attr temp_limit_attrs2[] = {
1401 .reg = PMBUS_UT_WARN_LIMIT,
1402 .low = true,
1403 .attr = "min",
1404 .alarm = "min_alarm",
1405 .sbit = PB_TEMP_UT_WARNING,
1406 }, {
1407 .reg = PMBUS_UT_FAULT_LIMIT,
1408 .low = true,
1409 .attr = "lcrit",
1410 .alarm = "lcrit_alarm",
1411 .sbit = PB_TEMP_UT_FAULT,
1412 }, {
1413 .reg = PMBUS_OT_WARN_LIMIT,
1414 .attr = "max",
1415 .alarm = "max_alarm",
1416 .sbit = PB_TEMP_OT_WARNING,
1417 }, {
1418 .reg = PMBUS_OT_FAULT_LIMIT,
1419 .attr = "crit",
1420 .alarm = "crit_alarm",
1421 .sbit = PB_TEMP_OT_FAULT,
1422 }, {
1423 .reg = PMBUS_VIRT_READ_TEMP2_MIN,
1424 .attr = "lowest",
1425 }, {
1426 .reg = PMBUS_VIRT_READ_TEMP2_MAX,
1427 .attr = "highest",
1428 }, {
1429 .reg = PMBUS_VIRT_RESET_TEMP2_HISTORY,
1430 .attr = "reset_history",
1434 static const struct pmbus_limit_attr temp_limit_attrs3[] = {
1436 .reg = PMBUS_UT_WARN_LIMIT,
1437 .low = true,
1438 .attr = "min",
1439 .alarm = "min_alarm",
1440 .sbit = PB_TEMP_UT_WARNING,
1441 }, {
1442 .reg = PMBUS_UT_FAULT_LIMIT,
1443 .low = true,
1444 .attr = "lcrit",
1445 .alarm = "lcrit_alarm",
1446 .sbit = PB_TEMP_UT_FAULT,
1447 }, {
1448 .reg = PMBUS_OT_WARN_LIMIT,
1449 .attr = "max",
1450 .alarm = "max_alarm",
1451 .sbit = PB_TEMP_OT_WARNING,
1452 }, {
1453 .reg = PMBUS_OT_FAULT_LIMIT,
1454 .attr = "crit",
1455 .alarm = "crit_alarm",
1456 .sbit = PB_TEMP_OT_FAULT,
1460 static const struct pmbus_sensor_attr temp_attributes[] = {
1462 .reg = PMBUS_READ_TEMPERATURE_1,
1463 .class = PSC_TEMPERATURE,
1464 .paged = true,
1465 .update = true,
1466 .compare = true,
1467 .func = PMBUS_HAVE_TEMP,
1468 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1469 .sbase = PB_STATUS_TEMP_BASE,
1470 .gbit = PB_STATUS_TEMPERATURE,
1471 .limit = temp_limit_attrs,
1472 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1473 }, {
1474 .reg = PMBUS_READ_TEMPERATURE_2,
1475 .class = PSC_TEMPERATURE,
1476 .paged = true,
1477 .update = true,
1478 .compare = true,
1479 .func = PMBUS_HAVE_TEMP2,
1480 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1481 .sbase = PB_STATUS_TEMP_BASE,
1482 .gbit = PB_STATUS_TEMPERATURE,
1483 .limit = temp_limit_attrs2,
1484 .nlimit = ARRAY_SIZE(temp_limit_attrs2),
1485 }, {
1486 .reg = PMBUS_READ_TEMPERATURE_3,
1487 .class = PSC_TEMPERATURE,
1488 .paged = true,
1489 .update = true,
1490 .compare = true,
1491 .func = PMBUS_HAVE_TEMP3,
1492 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1493 .sbase = PB_STATUS_TEMP_BASE,
1494 .gbit = PB_STATUS_TEMPERATURE,
1495 .limit = temp_limit_attrs3,
1496 .nlimit = ARRAY_SIZE(temp_limit_attrs3),
1500 static const int pmbus_fan_registers[] = {
1501 PMBUS_READ_FAN_SPEED_1,
1502 PMBUS_READ_FAN_SPEED_2,
1503 PMBUS_READ_FAN_SPEED_3,
1504 PMBUS_READ_FAN_SPEED_4
1507 static const int pmbus_fan_config_registers[] = {
1508 PMBUS_FAN_CONFIG_12,
1509 PMBUS_FAN_CONFIG_12,
1510 PMBUS_FAN_CONFIG_34,
1511 PMBUS_FAN_CONFIG_34
1514 static const int pmbus_fan_status_registers[] = {
1515 PMBUS_STATUS_FAN_12,
1516 PMBUS_STATUS_FAN_12,
1517 PMBUS_STATUS_FAN_34,
1518 PMBUS_STATUS_FAN_34
1521 static const u32 pmbus_fan_flags[] = {
1522 PMBUS_HAVE_FAN12,
1523 PMBUS_HAVE_FAN12,
1524 PMBUS_HAVE_FAN34,
1525 PMBUS_HAVE_FAN34
1528 static const u32 pmbus_fan_status_flags[] = {
1529 PMBUS_HAVE_STATUS_FAN12,
1530 PMBUS_HAVE_STATUS_FAN12,
1531 PMBUS_HAVE_STATUS_FAN34,
1532 PMBUS_HAVE_STATUS_FAN34
1535 /* Fans */
1536 static void pmbus_add_fan_attributes(struct i2c_client *client,
1537 struct pmbus_data *data)
1539 const struct pmbus_driver_info *info = data->info;
1540 int index = 1;
1541 int page;
1543 for (page = 0; page < info->pages; page++) {
1544 int f;
1546 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1547 int regval;
1549 if (!(info->func[page] & pmbus_fan_flags[f]))
1550 break;
1552 if (!pmbus_check_word_register(client, page,
1553 pmbus_fan_registers[f]))
1554 break;
1557 * Skip fan if not installed.
1558 * Each fan configuration register covers multiple fans,
1559 * so we have to do some magic.
1561 regval = _pmbus_read_byte_data(client, page,
1562 pmbus_fan_config_registers[f]);
1563 if (regval < 0 ||
1564 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1565 continue;
1567 pmbus_add_sensor(data, "fan", "input", index, page,
1568 pmbus_fan_registers[f], PSC_FAN, true,
1569 true);
1572 * Each fan status register covers multiple fans,
1573 * so we have to do some magic.
1575 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1576 pmbus_check_byte_register(client,
1577 page, pmbus_fan_status_registers[f])) {
1578 int base;
1580 if (f > 1) /* fan 3, 4 */
1581 base = PB_STATUS_FAN34_BASE + page;
1582 else
1583 base = PB_STATUS_FAN_BASE + page;
1584 pmbus_add_boolean_reg(data, "fan", "alarm",
1585 index, base,
1586 PB_FAN_FAN1_WARNING >> (f & 1));
1587 pmbus_add_boolean_reg(data, "fan", "fault",
1588 index, base,
1589 PB_FAN_FAN1_FAULT >> (f & 1));
1591 index++;
1596 static void pmbus_find_attributes(struct i2c_client *client,
1597 struct pmbus_data *data)
1599 /* Voltage sensors */
1600 pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
1601 ARRAY_SIZE(voltage_attributes));
1603 /* Current sensors */
1604 pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
1605 ARRAY_SIZE(current_attributes));
1607 /* Power sensors */
1608 pmbus_add_sensor_attrs(client, data, "power", power_attributes,
1609 ARRAY_SIZE(power_attributes));
1611 /* Temperature sensors */
1612 pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
1613 ARRAY_SIZE(temp_attributes));
1615 /* Fans */
1616 pmbus_add_fan_attributes(client, data);
1620 * Identify chip parameters.
1621 * This function is called for all chips.
1623 static int pmbus_identify_common(struct i2c_client *client,
1624 struct pmbus_data *data)
1626 int vout_mode = -1;
1628 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
1629 vout_mode = _pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1630 if (vout_mode >= 0 && vout_mode != 0xff) {
1632 * Not all chips support the VOUT_MODE command,
1633 * so a failure to read it is not an error.
1635 switch (vout_mode >> 5) {
1636 case 0: /* linear mode */
1637 if (data->info->format[PSC_VOLTAGE_OUT] != linear)
1638 return -ENODEV;
1640 data->exponent = ((s8)(vout_mode << 3)) >> 3;
1641 break;
1642 case 1: /* VID mode */
1643 if (data->info->format[PSC_VOLTAGE_OUT] != vid)
1644 return -ENODEV;
1645 break;
1646 case 2: /* direct mode */
1647 if (data->info->format[PSC_VOLTAGE_OUT] != direct)
1648 return -ENODEV;
1649 break;
1650 default:
1651 return -ENODEV;
1655 /* Determine maximum number of sensors, booleans, and labels */
1656 pmbus_find_max_attr(client, data);
1657 pmbus_clear_fault_page(client, 0);
1658 return 0;
1661 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
1662 struct pmbus_driver_info *info)
1664 const struct pmbus_platform_data *pdata = client->dev.platform_data;
1665 struct pmbus_data *data;
1666 int ret;
1668 if (!info) {
1669 dev_err(&client->dev, "Missing chip information");
1670 return -ENODEV;
1673 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
1674 | I2C_FUNC_SMBUS_BYTE_DATA
1675 | I2C_FUNC_SMBUS_WORD_DATA))
1676 return -ENODEV;
1678 data = kzalloc(sizeof(*data), GFP_KERNEL);
1679 if (!data) {
1680 dev_err(&client->dev, "No memory to allocate driver data\n");
1681 return -ENOMEM;
1684 i2c_set_clientdata(client, data);
1685 mutex_init(&data->update_lock);
1687 /* Bail out if PMBus status register does not exist. */
1688 if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0) {
1689 dev_err(&client->dev, "PMBus status register not found\n");
1690 ret = -ENODEV;
1691 goto out_data;
1694 if (pdata)
1695 data->flags = pdata->flags;
1696 data->info = info;
1698 pmbus_clear_faults(client);
1700 if (info->identify) {
1701 ret = (*info->identify)(client, info);
1702 if (ret < 0) {
1703 dev_err(&client->dev, "Chip identification failed\n");
1704 goto out_data;
1708 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1709 dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
1710 info->pages);
1711 ret = -ENODEV;
1712 goto out_data;
1715 ret = pmbus_identify_common(client, data);
1716 if (ret < 0) {
1717 dev_err(&client->dev, "Failed to identify chip capabilities\n");
1718 goto out_data;
1721 ret = -ENOMEM;
1722 data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors,
1723 GFP_KERNEL);
1724 if (!data->sensors) {
1725 dev_err(&client->dev, "No memory to allocate sensor data\n");
1726 goto out_data;
1729 data->booleans = kzalloc(sizeof(struct pmbus_boolean)
1730 * data->max_booleans, GFP_KERNEL);
1731 if (!data->booleans) {
1732 dev_err(&client->dev, "No memory to allocate boolean data\n");
1733 goto out_sensors;
1736 data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels,
1737 GFP_KERNEL);
1738 if (!data->labels) {
1739 dev_err(&client->dev, "No memory to allocate label data\n");
1740 goto out_booleans;
1743 data->attributes = kzalloc(sizeof(struct attribute *)
1744 * data->max_attributes, GFP_KERNEL);
1745 if (!data->attributes) {
1746 dev_err(&client->dev, "No memory to allocate attribute data\n");
1747 goto out_labels;
1750 pmbus_find_attributes(client, data);
1753 * If there are no attributes, something is wrong.
1754 * Bail out instead of trying to register nothing.
1756 if (!data->num_attributes) {
1757 dev_err(&client->dev, "No attributes found\n");
1758 ret = -ENODEV;
1759 goto out_attributes;
1762 /* Register sysfs hooks */
1763 data->group.attrs = data->attributes;
1764 ret = sysfs_create_group(&client->dev.kobj, &data->group);
1765 if (ret) {
1766 dev_err(&client->dev, "Failed to create sysfs entries\n");
1767 goto out_attributes;
1769 data->hwmon_dev = hwmon_device_register(&client->dev);
1770 if (IS_ERR(data->hwmon_dev)) {
1771 ret = PTR_ERR(data->hwmon_dev);
1772 dev_err(&client->dev, "Failed to register hwmon device\n");
1773 goto out_hwmon_device_register;
1775 return 0;
1777 out_hwmon_device_register:
1778 sysfs_remove_group(&client->dev.kobj, &data->group);
1779 out_attributes:
1780 kfree(data->attributes);
1781 out_labels:
1782 kfree(data->labels);
1783 out_booleans:
1784 kfree(data->booleans);
1785 out_sensors:
1786 kfree(data->sensors);
1787 out_data:
1788 kfree(data);
1789 return ret;
1791 EXPORT_SYMBOL_GPL(pmbus_do_probe);
1793 void pmbus_do_remove(struct i2c_client *client)
1795 struct pmbus_data *data = i2c_get_clientdata(client);
1796 hwmon_device_unregister(data->hwmon_dev);
1797 sysfs_remove_group(&client->dev.kobj, &data->group);
1798 kfree(data->attributes);
1799 kfree(data->labels);
1800 kfree(data->booleans);
1801 kfree(data->sensors);
1802 kfree(data);
1804 EXPORT_SYMBOL_GPL(pmbus_do_remove);
1806 MODULE_AUTHOR("Guenter Roeck");
1807 MODULE_DESCRIPTION("PMBus core driver");
1808 MODULE_LICENSE("GPL");