hwmon: (pmbus) Strengthen check for status register existence
[deliverable/linux.git] / drivers / hwmon / pmbus / pmbus_core.c
1 /*
2 * Hardware monitoring driver for PMBus devices
3 *
4 * Copyright (c) 2010, 2011 Ericsson AB.
5 *
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.
10 *
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.
15 *
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.
19 */
20
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"
32
33 /*
34 * Constants needed to determine number of sensors, booleans, and labels.
35 */
36 #define PMBUS_MAX_INPUT_SENSORS 11 /* 6*volt, 3*curr, 2*power */
37 #define PMBUS_VOUT_SENSORS_PER_PAGE 5 /* input, min, max, lcrit,
38 crit */
39 #define PMBUS_IOUT_SENSORS_PER_PAGE 4 /* input, min, max, crit */
40 #define PMBUS_POUT_SENSORS_PER_PAGE 4 /* input, cap, max, crit */
41 #define PMBUS_MAX_SENSORS_PER_FAN 1 /* input */
42 #define PMBUS_MAX_SENSORS_PER_TEMP 5 /* input, min, max, lcrit,
43 crit */
44
45 #define PMBUS_MAX_INPUT_BOOLEANS 7 /* v: min_alarm, max_alarm,
46 lcrit_alarm, crit_alarm;
47 c: alarm, crit_alarm;
48 p: crit_alarm */
49 #define PMBUS_VOUT_BOOLEANS_PER_PAGE 4 /* min_alarm, max_alarm,
50 lcrit_alarm, crit_alarm */
51 #define PMBUS_IOUT_BOOLEANS_PER_PAGE 3 /* alarm, lcrit_alarm,
52 crit_alarm */
53 #define PMBUS_POUT_BOOLEANS_PER_PAGE 2 /* alarm, crit_alarm */
54 #define PMBUS_MAX_BOOLEANS_PER_FAN 2 /* alarm, fault */
55 #define PMBUS_MAX_BOOLEANS_PER_TEMP 4 /* min_alarm, max_alarm,
56 lcrit_alarm, crit_alarm */
57
58 #define PMBUS_MAX_INPUT_LABELS 4 /* vin, vcap, iin, pin */
59
60 /*
61 * status, status_vout, status_iout, status_fans, status_fan34, and status_temp
62 * are paged. status_input is unpaged.
63 */
64 #define PB_NUM_STATUS_REG (PMBUS_PAGES * 6 + 1)
65
66 /*
67 * Index into status register array, per status register group
68 */
69 #define PB_STATUS_BASE 0
70 #define PB_STATUS_VOUT_BASE (PB_STATUS_BASE + PMBUS_PAGES)
71 #define PB_STATUS_IOUT_BASE (PB_STATUS_VOUT_BASE + PMBUS_PAGES)
72 #define PB_STATUS_FAN_BASE (PB_STATUS_IOUT_BASE + PMBUS_PAGES)
73 #define PB_STATUS_FAN34_BASE (PB_STATUS_FAN_BASE + PMBUS_PAGES)
74 #define PB_STATUS_INPUT_BASE (PB_STATUS_FAN34_BASE + PMBUS_PAGES)
75 #define PB_STATUS_TEMP_BASE (PB_STATUS_INPUT_BASE + 1)
76
77 #define PMBUS_NAME_SIZE 24
78
79 struct pmbus_sensor {
80 char name[PMBUS_NAME_SIZE]; /* sysfs sensor name */
81 struct sensor_device_attribute attribute;
82 u8 page; /* page number */
83 u8 reg; /* register */
84 enum pmbus_sensor_classes class; /* sensor class */
85 bool update; /* runtime sensor update needed */
86 int data; /* Sensor data.
87 Negative if there was a read error */
88 };
89
90 struct pmbus_boolean {
91 char name[PMBUS_NAME_SIZE]; /* sysfs boolean name */
92 struct sensor_device_attribute attribute;
93 };
94
95 struct pmbus_label {
96 char name[PMBUS_NAME_SIZE]; /* sysfs label name */
97 struct sensor_device_attribute attribute;
98 char label[PMBUS_NAME_SIZE]; /* label */
99 };
100
101 struct pmbus_data {
102 struct device *hwmon_dev;
103
104 u32 flags; /* from platform data */
105
106 int exponent; /* linear mode: exponent for output voltages */
107
108 const struct pmbus_driver_info *info;
109
110 int max_attributes;
111 int num_attributes;
112 struct attribute **attributes;
113 struct attribute_group group;
114
115 /*
116 * Sensors cover both sensor and limit registers.
117 */
118 int max_sensors;
119 int num_sensors;
120 struct pmbus_sensor *sensors;
121 /*
122 * Booleans are used for alarms.
123 * Values are determined from status registers.
124 */
125 int max_booleans;
126 int num_booleans;
127 struct pmbus_boolean *booleans;
128 /*
129 * Labels are used to map generic names (e.g., "in1")
130 * to PMBus specific names (e.g., "vin" or "vout1").
131 */
132 int max_labels;
133 int num_labels;
134 struct pmbus_label *labels;
135
136 struct mutex update_lock;
137 bool valid;
138 unsigned long last_updated; /* in jiffies */
139
140 /*
141 * A single status register covers multiple attributes,
142 * so we keep them all together.
143 */
144 u8 status[PB_NUM_STATUS_REG];
145
146 u8 currpage;
147 };
148
149 int pmbus_set_page(struct i2c_client *client, u8 page)
150 {
151 struct pmbus_data *data = i2c_get_clientdata(client);
152 int rv = 0;
153 int newpage;
154
155 if (page != data->currpage) {
156 rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
157 newpage = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
158 if (newpage != page)
159 rv = -EINVAL;
160 else
161 data->currpage = page;
162 }
163 return rv;
164 }
165 EXPORT_SYMBOL_GPL(pmbus_set_page);
166
167 static int pmbus_write_byte(struct i2c_client *client, int page, u8 value)
168 {
169 int rv;
170
171 if (page >= 0) {
172 rv = pmbus_set_page(client, page);
173 if (rv < 0)
174 return rv;
175 }
176
177 return i2c_smbus_write_byte(client, value);
178 }
179
180 int pmbus_write_word_data(struct i2c_client *client, u8 page, u8 reg, u16 word)
181 {
182 int rv;
183
184 rv = pmbus_set_page(client, page);
185 if (rv < 0)
186 return rv;
187
188 return i2c_smbus_write_word_data(client, reg, word);
189 }
190 EXPORT_SYMBOL_GPL(pmbus_write_word_data);
191
192 /*
193 * _pmbus_write_word_data() is similar to pmbus_write_word_data(), but checks if
194 * a device specific mapping function exists and calls it if necessary.
195 */
196 static int _pmbus_write_word_data(struct i2c_client *client, int page, int reg,
197 u16 word)
198 {
199 struct pmbus_data *data = i2c_get_clientdata(client);
200 const struct pmbus_driver_info *info = data->info;
201 int status;
202
203 if (info->write_word_data) {
204 status = info->write_word_data(client, page, reg, word);
205 if (status != -ENODATA)
206 return status;
207 }
208 if (reg >= PMBUS_VIRT_BASE)
209 return -EINVAL;
210 return pmbus_write_word_data(client, page, reg, word);
211 }
212
213 int pmbus_read_word_data(struct i2c_client *client, u8 page, u8 reg)
214 {
215 int rv;
216
217 rv = pmbus_set_page(client, page);
218 if (rv < 0)
219 return rv;
220
221 return i2c_smbus_read_word_data(client, reg);
222 }
223 EXPORT_SYMBOL_GPL(pmbus_read_word_data);
224
225 /*
226 * _pmbus_read_word_data() is similar to pmbus_read_word_data(), but checks if
227 * a device specific mapping function exists and calls it if necessary.
228 */
229 static int _pmbus_read_word_data(struct i2c_client *client, int page, int reg)
230 {
231 struct pmbus_data *data = i2c_get_clientdata(client);
232 const struct pmbus_driver_info *info = data->info;
233 int status;
234
235 if (info->read_word_data) {
236 status = info->read_word_data(client, page, reg);
237 if (status != -ENODATA)
238 return status;
239 }
240 return pmbus_read_word_data(client, page, reg);
241 }
242
243 int pmbus_read_byte_data(struct i2c_client *client, int page, u8 reg)
244 {
245 int rv;
246
247 if (page >= 0) {
248 rv = pmbus_set_page(client, page);
249 if (rv < 0)
250 return rv;
251 }
252
253 return i2c_smbus_read_byte_data(client, reg);
254 }
255 EXPORT_SYMBOL_GPL(pmbus_read_byte_data);
256
257 /*
258 * _pmbus_read_byte_data() is similar to pmbus_read_byte_data(), but checks if
259 * a device specific mapping function exists and calls it if necessary.
260 */
261 static int _pmbus_read_byte_data(struct i2c_client *client, int page, int reg)
262 {
263 struct pmbus_data *data = i2c_get_clientdata(client);
264 const struct pmbus_driver_info *info = data->info;
265 int status;
266
267 if (info->read_byte_data) {
268 status = info->read_byte_data(client, page, reg);
269 if (status != -ENODATA)
270 return status;
271 }
272 return pmbus_read_byte_data(client, page, reg);
273 }
274
275 static void pmbus_clear_fault_page(struct i2c_client *client, int page)
276 {
277 pmbus_write_byte(client, page, PMBUS_CLEAR_FAULTS);
278 }
279
280 void pmbus_clear_faults(struct i2c_client *client)
281 {
282 struct pmbus_data *data = i2c_get_clientdata(client);
283 int i;
284
285 for (i = 0; i < data->info->pages; i++)
286 pmbus_clear_fault_page(client, i);
287 }
288 EXPORT_SYMBOL_GPL(pmbus_clear_faults);
289
290 static int pmbus_check_status_cml(struct i2c_client *client)
291 {
292 int status, status2;
293
294 status = pmbus_read_byte_data(client, -1, PMBUS_STATUS_BYTE);
295 if (status < 0 || (status & PB_STATUS_CML)) {
296 status2 = pmbus_read_byte_data(client, -1, PMBUS_STATUS_CML);
297 if (status2 < 0 || (status2 & PB_CML_FAULT_INVALID_COMMAND))
298 return -EINVAL;
299 }
300 return 0;
301 }
302
303 bool pmbus_check_byte_register(struct i2c_client *client, int page, int reg)
304 {
305 int rv;
306 struct pmbus_data *data = i2c_get_clientdata(client);
307
308 rv = _pmbus_read_byte_data(client, page, reg);
309 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
310 rv = pmbus_check_status_cml(client);
311 pmbus_clear_fault_page(client, -1);
312 return rv >= 0;
313 }
314 EXPORT_SYMBOL_GPL(pmbus_check_byte_register);
315
316 bool pmbus_check_word_register(struct i2c_client *client, int page, int reg)
317 {
318 int rv;
319 struct pmbus_data *data = i2c_get_clientdata(client);
320
321 rv = pmbus_read_word_data(client, page, reg);
322 if (rv >= 0 && !(data->flags & PMBUS_SKIP_STATUS_CHECK))
323 rv = pmbus_check_status_cml(client);
324 pmbus_clear_fault_page(client, -1);
325 return rv >= 0;
326 }
327 EXPORT_SYMBOL_GPL(pmbus_check_word_register);
328
329 const struct pmbus_driver_info *pmbus_get_driver_info(struct i2c_client *client)
330 {
331 struct pmbus_data *data = i2c_get_clientdata(client);
332
333 return data->info;
334 }
335 EXPORT_SYMBOL_GPL(pmbus_get_driver_info);
336
337 static struct pmbus_data *pmbus_update_device(struct device *dev)
338 {
339 struct i2c_client *client = to_i2c_client(dev);
340 struct pmbus_data *data = i2c_get_clientdata(client);
341 const struct pmbus_driver_info *info = data->info;
342
343 mutex_lock(&data->update_lock);
344 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
345 int i;
346
347 for (i = 0; i < info->pages; i++)
348 data->status[PB_STATUS_BASE + i]
349 = pmbus_read_byte_data(client, i,
350 PMBUS_STATUS_BYTE);
351 for (i = 0; i < info->pages; i++) {
352 if (!(info->func[i] & PMBUS_HAVE_STATUS_VOUT))
353 continue;
354 data->status[PB_STATUS_VOUT_BASE + i]
355 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_VOUT);
356 }
357 for (i = 0; i < info->pages; i++) {
358 if (!(info->func[i] & PMBUS_HAVE_STATUS_IOUT))
359 continue;
360 data->status[PB_STATUS_IOUT_BASE + i]
361 = _pmbus_read_byte_data(client, i, PMBUS_STATUS_IOUT);
362 }
363 for (i = 0; i < info->pages; i++) {
364 if (!(info->func[i] & PMBUS_HAVE_STATUS_TEMP))
365 continue;
366 data->status[PB_STATUS_TEMP_BASE + i]
367 = _pmbus_read_byte_data(client, i,
368 PMBUS_STATUS_TEMPERATURE);
369 }
370 for (i = 0; i < info->pages; i++) {
371 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN12))
372 continue;
373 data->status[PB_STATUS_FAN_BASE + i]
374 = _pmbus_read_byte_data(client, i,
375 PMBUS_STATUS_FAN_12);
376 }
377
378 for (i = 0; i < info->pages; i++) {
379 if (!(info->func[i] & PMBUS_HAVE_STATUS_FAN34))
380 continue;
381 data->status[PB_STATUS_FAN34_BASE + i]
382 = _pmbus_read_byte_data(client, i,
383 PMBUS_STATUS_FAN_34);
384 }
385
386 if (info->func[0] & PMBUS_HAVE_STATUS_INPUT)
387 data->status[PB_STATUS_INPUT_BASE]
388 = _pmbus_read_byte_data(client, 0,
389 PMBUS_STATUS_INPUT);
390
391 for (i = 0; i < data->num_sensors; i++) {
392 struct pmbus_sensor *sensor = &data->sensors[i];
393
394 if (!data->valid || sensor->update)
395 sensor->data
396 = _pmbus_read_word_data(client,
397 sensor->page,
398 sensor->reg);
399 }
400 pmbus_clear_faults(client);
401 data->last_updated = jiffies;
402 data->valid = 1;
403 }
404 mutex_unlock(&data->update_lock);
405 return data;
406 }
407
408 /*
409 * Convert linear sensor values to milli- or micro-units
410 * depending on sensor type.
411 */
412 static long pmbus_reg2data_linear(struct pmbus_data *data,
413 struct pmbus_sensor *sensor)
414 {
415 s16 exponent;
416 s32 mantissa;
417 long val;
418
419 if (sensor->class == PSC_VOLTAGE_OUT) { /* LINEAR16 */
420 exponent = data->exponent;
421 mantissa = (u16) sensor->data;
422 } else { /* LINEAR11 */
423 exponent = (sensor->data >> 11) & 0x001f;
424 mantissa = sensor->data & 0x07ff;
425
426 if (exponent > 0x0f)
427 exponent |= 0xffe0; /* sign extend exponent */
428 if (mantissa > 0x03ff)
429 mantissa |= 0xfffff800; /* sign extend mantissa */
430 }
431
432 val = mantissa;
433
434 /* scale result to milli-units for all sensors except fans */
435 if (sensor->class != PSC_FAN)
436 val = val * 1000L;
437
438 /* scale result to micro-units for power sensors */
439 if (sensor->class == PSC_POWER)
440 val = val * 1000L;
441
442 if (exponent >= 0)
443 val <<= exponent;
444 else
445 val >>= -exponent;
446
447 return val;
448 }
449
450 /*
451 * Convert direct sensor values to milli- or micro-units
452 * depending on sensor type.
453 */
454 static long pmbus_reg2data_direct(struct pmbus_data *data,
455 struct pmbus_sensor *sensor)
456 {
457 long val = (s16) sensor->data;
458 long m, b, R;
459
460 m = data->info->m[sensor->class];
461 b = data->info->b[sensor->class];
462 R = data->info->R[sensor->class];
463
464 if (m == 0)
465 return 0;
466
467 /* X = 1/m * (Y * 10^-R - b) */
468 R = -R;
469 /* scale result to milli-units for everything but fans */
470 if (sensor->class != PSC_FAN) {
471 R += 3;
472 b *= 1000;
473 }
474
475 /* scale result to micro-units for power sensors */
476 if (sensor->class == PSC_POWER) {
477 R += 3;
478 b *= 1000;
479 }
480
481 while (R > 0) {
482 val *= 10;
483 R--;
484 }
485 while (R < 0) {
486 val = DIV_ROUND_CLOSEST(val, 10);
487 R++;
488 }
489
490 return (val - b) / m;
491 }
492
493 /*
494 * Convert VID sensor values to milli- or micro-units
495 * depending on sensor type.
496 * We currently only support VR11.
497 */
498 static long pmbus_reg2data_vid(struct pmbus_data *data,
499 struct pmbus_sensor *sensor)
500 {
501 long val = sensor->data;
502
503 if (val < 0x02 || val > 0xb2)
504 return 0;
505 return DIV_ROUND_CLOSEST(160000 - (val - 2) * 625, 100);
506 }
507
508 static long pmbus_reg2data(struct pmbus_data *data, struct pmbus_sensor *sensor)
509 {
510 long val;
511
512 switch (data->info->format[sensor->class]) {
513 case direct:
514 val = pmbus_reg2data_direct(data, sensor);
515 break;
516 case vid:
517 val = pmbus_reg2data_vid(data, sensor);
518 break;
519 case linear:
520 default:
521 val = pmbus_reg2data_linear(data, sensor);
522 break;
523 }
524 return val;
525 }
526
527 #define MAX_MANTISSA (1023 * 1000)
528 #define MIN_MANTISSA (511 * 1000)
529
530 static u16 pmbus_data2reg_linear(struct pmbus_data *data,
531 enum pmbus_sensor_classes class, long val)
532 {
533 s16 exponent = 0, mantissa;
534 bool negative = false;
535
536 /* simple case */
537 if (val == 0)
538 return 0;
539
540 if (class == PSC_VOLTAGE_OUT) {
541 /* LINEAR16 does not support negative voltages */
542 if (val < 0)
543 return 0;
544
545 /*
546 * For a static exponents, we don't have a choice
547 * but to adjust the value to it.
548 */
549 if (data->exponent < 0)
550 val <<= -data->exponent;
551 else
552 val >>= data->exponent;
553 val = DIV_ROUND_CLOSEST(val, 1000);
554 return val & 0xffff;
555 }
556
557 if (val < 0) {
558 negative = true;
559 val = -val;
560 }
561
562 /* Power is in uW. Convert to mW before converting. */
563 if (class == PSC_POWER)
564 val = DIV_ROUND_CLOSEST(val, 1000L);
565
566 /*
567 * For simplicity, convert fan data to milli-units
568 * before calculating the exponent.
569 */
570 if (class == PSC_FAN)
571 val = val * 1000;
572
573 /* Reduce large mantissa until it fits into 10 bit */
574 while (val >= MAX_MANTISSA && exponent < 15) {
575 exponent++;
576 val >>= 1;
577 }
578 /* Increase small mantissa to improve precision */
579 while (val < MIN_MANTISSA && exponent > -15) {
580 exponent--;
581 val <<= 1;
582 }
583
584 /* Convert mantissa from milli-units to units */
585 mantissa = DIV_ROUND_CLOSEST(val, 1000);
586
587 /* Ensure that resulting number is within range */
588 if (mantissa > 0x3ff)
589 mantissa = 0x3ff;
590
591 /* restore sign */
592 if (negative)
593 mantissa = -mantissa;
594
595 /* Convert to 5 bit exponent, 11 bit mantissa */
596 return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
597 }
598
599 static u16 pmbus_data2reg_direct(struct pmbus_data *data,
600 enum pmbus_sensor_classes class, long val)
601 {
602 long m, b, R;
603
604 m = data->info->m[class];
605 b = data->info->b[class];
606 R = data->info->R[class];
607
608 /* Power is in uW. Adjust R and b. */
609 if (class == PSC_POWER) {
610 R -= 3;
611 b *= 1000;
612 }
613
614 /* Calculate Y = (m * X + b) * 10^R */
615 if (class != PSC_FAN) {
616 R -= 3; /* Adjust R and b for data in milli-units */
617 b *= 1000;
618 }
619 val = val * m + b;
620
621 while (R > 0) {
622 val *= 10;
623 R--;
624 }
625 while (R < 0) {
626 val = DIV_ROUND_CLOSEST(val, 10);
627 R++;
628 }
629
630 return val;
631 }
632
633 static u16 pmbus_data2reg_vid(struct pmbus_data *data,
634 enum pmbus_sensor_classes class, long val)
635 {
636 val = SENSORS_LIMIT(val, 500, 1600);
637
638 return 2 + DIV_ROUND_CLOSEST((1600 - val) * 100, 625);
639 }
640
641 static u16 pmbus_data2reg(struct pmbus_data *data,
642 enum pmbus_sensor_classes class, long val)
643 {
644 u16 regval;
645
646 switch (data->info->format[class]) {
647 case direct:
648 regval = pmbus_data2reg_direct(data, class, val);
649 break;
650 case vid:
651 regval = pmbus_data2reg_vid(data, class, val);
652 break;
653 case linear:
654 default:
655 regval = pmbus_data2reg_linear(data, class, val);
656 break;
657 }
658 return regval;
659 }
660
661 /*
662 * Return boolean calculated from converted data.
663 * <index> defines a status register index and mask, and optionally
664 * two sensor indexes.
665 * The upper half-word references the two sensors,
666 * two sensor indices.
667 * The upper half-word references the two optional sensors,
668 * the lower half word references status register and mask.
669 * The function returns true if (status[reg] & mask) is true and,
670 * if specified, if v1 >= v2.
671 * To determine if an object exceeds upper limits, specify <v, limit>.
672 * To determine if an object exceeds lower limits, specify <limit, v>.
673 *
674 * For booleans created with pmbus_add_boolean_reg(), only the lower 16 bits of
675 * index are set. s1 and s2 (the sensor index values) are zero in this case.
676 * The function returns true if (status[reg] & mask) is true.
677 *
678 * If the boolean was created with pmbus_add_boolean_cmp(), a comparison against
679 * a specified limit has to be performed to determine the boolean result.
680 * In this case, the function returns true if v1 >= v2 (where v1 and v2 are
681 * sensor values referenced by sensor indices s1 and s2).
682 *
683 * To determine if an object exceeds upper limits, specify <s1,s2> = <v,limit>.
684 * To determine if an object exceeds lower limits, specify <s1,s2> = <limit,v>.
685 *
686 * If a negative value is stored in any of the referenced registers, this value
687 * reflects an error code which will be returned.
688 */
689 static int pmbus_get_boolean(struct pmbus_data *data, int index, int *val)
690 {
691 u8 s1 = (index >> 24) & 0xff;
692 u8 s2 = (index >> 16) & 0xff;
693 u8 reg = (index >> 8) & 0xff;
694 u8 mask = index & 0xff;
695 int status;
696 u8 regval;
697
698 status = data->status[reg];
699 if (status < 0)
700 return status;
701
702 regval = status & mask;
703 if (!s1 && !s2)
704 *val = !!regval;
705 else {
706 long v1, v2;
707 struct pmbus_sensor *sensor1, *sensor2;
708
709 sensor1 = &data->sensors[s1];
710 if (sensor1->data < 0)
711 return sensor1->data;
712 sensor2 = &data->sensors[s2];
713 if (sensor2->data < 0)
714 return sensor2->data;
715
716 v1 = pmbus_reg2data(data, sensor1);
717 v2 = pmbus_reg2data(data, sensor2);
718 *val = !!(regval && v1 >= v2);
719 }
720 return 0;
721 }
722
723 static ssize_t pmbus_show_boolean(struct device *dev,
724 struct device_attribute *da, char *buf)
725 {
726 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
727 struct pmbus_data *data = pmbus_update_device(dev);
728 int val;
729 int err;
730
731 err = pmbus_get_boolean(data, attr->index, &val);
732 if (err)
733 return err;
734 return snprintf(buf, PAGE_SIZE, "%d\n", val);
735 }
736
737 static ssize_t pmbus_show_sensor(struct device *dev,
738 struct device_attribute *da, char *buf)
739 {
740 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
741 struct pmbus_data *data = pmbus_update_device(dev);
742 struct pmbus_sensor *sensor;
743
744 sensor = &data->sensors[attr->index];
745 if (sensor->data < 0)
746 return sensor->data;
747
748 return snprintf(buf, PAGE_SIZE, "%ld\n", pmbus_reg2data(data, sensor));
749 }
750
751 static ssize_t pmbus_set_sensor(struct device *dev,
752 struct device_attribute *devattr,
753 const char *buf, size_t count)
754 {
755 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
756 struct i2c_client *client = to_i2c_client(dev);
757 struct pmbus_data *data = i2c_get_clientdata(client);
758 struct pmbus_sensor *sensor = &data->sensors[attr->index];
759 ssize_t rv = count;
760 long val = 0;
761 int ret;
762 u16 regval;
763
764 if (strict_strtol(buf, 10, &val) < 0)
765 return -EINVAL;
766
767 mutex_lock(&data->update_lock);
768 regval = pmbus_data2reg(data, sensor->class, val);
769 ret = _pmbus_write_word_data(client, sensor->page, sensor->reg, regval);
770 if (ret < 0)
771 rv = ret;
772 else
773 data->sensors[attr->index].data = regval;
774 mutex_unlock(&data->update_lock);
775 return rv;
776 }
777
778 static ssize_t pmbus_show_label(struct device *dev,
779 struct device_attribute *da, char *buf)
780 {
781 struct i2c_client *client = to_i2c_client(dev);
782 struct pmbus_data *data = i2c_get_clientdata(client);
783 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
784
785 return snprintf(buf, PAGE_SIZE, "%s\n",
786 data->labels[attr->index].label);
787 }
788
789 #define PMBUS_ADD_ATTR(data, _name, _idx, _mode, _type, _show, _set) \
790 do { \
791 struct sensor_device_attribute *a \
792 = &data->_type##s[data->num_##_type##s].attribute; \
793 BUG_ON(data->num_attributes >= data->max_attributes); \
794 sysfs_attr_init(&a->dev_attr.attr); \
795 a->dev_attr.attr.name = _name; \
796 a->dev_attr.attr.mode = _mode; \
797 a->dev_attr.show = _show; \
798 a->dev_attr.store = _set; \
799 a->index = _idx; \
800 data->attributes[data->num_attributes] = &a->dev_attr.attr; \
801 data->num_attributes++; \
802 } while (0)
803
804 #define PMBUS_ADD_GET_ATTR(data, _name, _type, _idx) \
805 PMBUS_ADD_ATTR(data, _name, _idx, S_IRUGO, _type, \
806 pmbus_show_##_type, NULL)
807
808 #define PMBUS_ADD_SET_ATTR(data, _name, _type, _idx) \
809 PMBUS_ADD_ATTR(data, _name, _idx, S_IWUSR | S_IRUGO, _type, \
810 pmbus_show_##_type, pmbus_set_##_type)
811
812 static void pmbus_add_boolean(struct pmbus_data *data,
813 const char *name, const char *type, int seq,
814 int idx)
815 {
816 struct pmbus_boolean *boolean;
817
818 BUG_ON(data->num_booleans >= data->max_booleans);
819
820 boolean = &data->booleans[data->num_booleans];
821
822 snprintf(boolean->name, sizeof(boolean->name), "%s%d_%s",
823 name, seq, type);
824 PMBUS_ADD_GET_ATTR(data, boolean->name, boolean, idx);
825 data->num_booleans++;
826 }
827
828 static void pmbus_add_boolean_reg(struct pmbus_data *data,
829 const char *name, const char *type,
830 int seq, int reg, int bit)
831 {
832 pmbus_add_boolean(data, name, type, seq, (reg << 8) | bit);
833 }
834
835 static void pmbus_add_boolean_cmp(struct pmbus_data *data,
836 const char *name, const char *type,
837 int seq, int i1, int i2, int reg, int mask)
838 {
839 pmbus_add_boolean(data, name, type, seq,
840 (i1 << 24) | (i2 << 16) | (reg << 8) | mask);
841 }
842
843 static void pmbus_add_sensor(struct pmbus_data *data,
844 const char *name, const char *type, int seq,
845 int page, int reg, enum pmbus_sensor_classes class,
846 bool update, bool readonly)
847 {
848 struct pmbus_sensor *sensor;
849
850 BUG_ON(data->num_sensors >= data->max_sensors);
851
852 sensor = &data->sensors[data->num_sensors];
853 snprintf(sensor->name, sizeof(sensor->name), "%s%d_%s",
854 name, seq, type);
855 sensor->page = page;
856 sensor->reg = reg;
857 sensor->class = class;
858 sensor->update = update;
859 if (readonly)
860 PMBUS_ADD_GET_ATTR(data, sensor->name, sensor,
861 data->num_sensors);
862 else
863 PMBUS_ADD_SET_ATTR(data, sensor->name, sensor,
864 data->num_sensors);
865 data->num_sensors++;
866 }
867
868 static void pmbus_add_label(struct pmbus_data *data,
869 const char *name, int seq,
870 const char *lstring, int index)
871 {
872 struct pmbus_label *label;
873
874 BUG_ON(data->num_labels >= data->max_labels);
875
876 label = &data->labels[data->num_labels];
877 snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
878 if (!index)
879 strncpy(label->label, lstring, sizeof(label->label) - 1);
880 else
881 snprintf(label->label, sizeof(label->label), "%s%d", lstring,
882 index);
883
884 PMBUS_ADD_GET_ATTR(data, label->name, label, data->num_labels);
885 data->num_labels++;
886 }
887
888 /*
889 * Determine maximum number of sensors, booleans, and labels.
890 * To keep things simple, only make a rough high estimate.
891 */
892 static void pmbus_find_max_attr(struct i2c_client *client,
893 struct pmbus_data *data)
894 {
895 const struct pmbus_driver_info *info = data->info;
896 int page, max_sensors, max_booleans, max_labels;
897
898 max_sensors = PMBUS_MAX_INPUT_SENSORS;
899 max_booleans = PMBUS_MAX_INPUT_BOOLEANS;
900 max_labels = PMBUS_MAX_INPUT_LABELS;
901
902 for (page = 0; page < info->pages; page++) {
903 if (info->func[page] & PMBUS_HAVE_VOUT) {
904 max_sensors += PMBUS_VOUT_SENSORS_PER_PAGE;
905 max_booleans += PMBUS_VOUT_BOOLEANS_PER_PAGE;
906 max_labels++;
907 }
908 if (info->func[page] & PMBUS_HAVE_IOUT) {
909 max_sensors += PMBUS_IOUT_SENSORS_PER_PAGE;
910 max_booleans += PMBUS_IOUT_BOOLEANS_PER_PAGE;
911 max_labels++;
912 }
913 if (info->func[page] & PMBUS_HAVE_POUT) {
914 max_sensors += PMBUS_POUT_SENSORS_PER_PAGE;
915 max_booleans += PMBUS_POUT_BOOLEANS_PER_PAGE;
916 max_labels++;
917 }
918 if (info->func[page] & PMBUS_HAVE_FAN12) {
919 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
920 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
921 }
922 if (info->func[page] & PMBUS_HAVE_FAN34) {
923 max_sensors += 2 * PMBUS_MAX_SENSORS_PER_FAN;
924 max_booleans += 2 * PMBUS_MAX_BOOLEANS_PER_FAN;
925 }
926 if (info->func[page] & PMBUS_HAVE_TEMP) {
927 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
928 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
929 }
930 if (info->func[page] & PMBUS_HAVE_TEMP2) {
931 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
932 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
933 }
934 if (info->func[page] & PMBUS_HAVE_TEMP3) {
935 max_sensors += PMBUS_MAX_SENSORS_PER_TEMP;
936 max_booleans += PMBUS_MAX_BOOLEANS_PER_TEMP;
937 }
938 }
939 data->max_sensors = max_sensors;
940 data->max_booleans = max_booleans;
941 data->max_labels = max_labels;
942 data->max_attributes = max_sensors + max_booleans + max_labels;
943 }
944
945 /*
946 * Search for attributes. Allocate sensors, booleans, and labels as needed.
947 */
948
949 /*
950 * The pmbus_limit_attr structure describes a single limit attribute
951 * and its associated alarm attribute.
952 */
953 struct pmbus_limit_attr {
954 u8 reg; /* Limit register */
955 const char *attr; /* Attribute name */
956 const char *alarm; /* Alarm attribute name */
957 u32 sbit; /* Alarm attribute status bit */
958 };
959
960 /*
961 * The pmbus_sensor_attr structure describes one sensor attribute. This
962 * description includes a reference to the associated limit attributes.
963 */
964 struct pmbus_sensor_attr {
965 u8 reg; /* sensor register */
966 enum pmbus_sensor_classes class;/* sensor class */
967 const char *label; /* sensor label */
968 bool paged; /* true if paged sensor */
969 bool update; /* true if update needed */
970 bool compare; /* true if compare function needed */
971 u32 func; /* sensor mask */
972 u32 sfunc; /* sensor status mask */
973 int sbase; /* status base register */
974 u32 gbit; /* generic status bit */
975 const struct pmbus_limit_attr *limit;/* limit registers */
976 int nlimit; /* # of limit registers */
977 };
978
979 /*
980 * Add a set of limit attributes and, if supported, the associated
981 * alarm attributes.
982 */
983 static bool pmbus_add_limit_attrs(struct i2c_client *client,
984 struct pmbus_data *data,
985 const struct pmbus_driver_info *info,
986 const char *name, int index, int page,
987 int cbase,
988 const struct pmbus_sensor_attr *attr)
989 {
990 const struct pmbus_limit_attr *l = attr->limit;
991 int nlimit = attr->nlimit;
992 bool have_alarm = false;
993 int i, cindex;
994
995 for (i = 0; i < nlimit; i++) {
996 if (pmbus_check_word_register(client, page, l->reg)) {
997 cindex = data->num_sensors;
998 pmbus_add_sensor(data, name, l->attr, index, page,
999 l->reg, attr->class, attr->update,
1000 false);
1001 if (info->func[page] & attr->sfunc) {
1002 if (attr->compare) {
1003 pmbus_add_boolean_cmp(data, name,
1004 l->alarm, index,
1005 cbase, cindex,
1006 attr->sbase + page, l->sbit);
1007 } else {
1008 pmbus_add_boolean_reg(data, name,
1009 l->alarm, index,
1010 attr->sbase + page, l->sbit);
1011 }
1012 have_alarm = true;
1013 }
1014 }
1015 l++;
1016 }
1017 return have_alarm;
1018 }
1019
1020 static void pmbus_add_sensor_attrs_one(struct i2c_client *client,
1021 struct pmbus_data *data,
1022 const struct pmbus_driver_info *info,
1023 const char *name,
1024 int index, int page,
1025 const struct pmbus_sensor_attr *attr)
1026 {
1027 bool have_alarm;
1028 int cbase = data->num_sensors;
1029
1030 if (attr->label)
1031 pmbus_add_label(data, name, index, attr->label,
1032 attr->paged ? page + 1 : 0);
1033 pmbus_add_sensor(data, name, "input", index, page, attr->reg,
1034 attr->class, true, true);
1035 if (attr->sfunc) {
1036 have_alarm = pmbus_add_limit_attrs(client, data, info, name,
1037 index, page, cbase, attr);
1038 /*
1039 * Add generic alarm attribute only if there are no individual
1040 * alarm attributes, if there is a global alarm bit, and if
1041 * the generic status register for this page is accessible.
1042 */
1043 if (!have_alarm && attr->gbit &&
1044 pmbus_check_byte_register(client, page, PMBUS_STATUS_BYTE))
1045 pmbus_add_boolean_reg(data, name, "alarm", index,
1046 PB_STATUS_BASE + page,
1047 attr->gbit);
1048 }
1049 }
1050
1051 static void pmbus_add_sensor_attrs(struct i2c_client *client,
1052 struct pmbus_data *data,
1053 const char *name,
1054 const struct pmbus_sensor_attr *attrs,
1055 int nattrs)
1056 {
1057 const struct pmbus_driver_info *info = data->info;
1058 int index, i;
1059
1060 index = 1;
1061 for (i = 0; i < nattrs; i++) {
1062 int page, pages;
1063
1064 pages = attrs->paged ? info->pages : 1;
1065 for (page = 0; page < pages; page++) {
1066 if (!(info->func[page] & attrs->func))
1067 continue;
1068 pmbus_add_sensor_attrs_one(client, data, info, name,
1069 index, page, attrs);
1070 index++;
1071 }
1072 attrs++;
1073 }
1074 }
1075
1076 static const struct pmbus_limit_attr vin_limit_attrs[] = {
1077 {
1078 .reg = PMBUS_VIN_UV_WARN_LIMIT,
1079 .attr = "min",
1080 .alarm = "min_alarm",
1081 .sbit = PB_VOLTAGE_UV_WARNING,
1082 }, {
1083 .reg = PMBUS_VIN_UV_FAULT_LIMIT,
1084 .attr = "lcrit",
1085 .alarm = "lcrit_alarm",
1086 .sbit = PB_VOLTAGE_UV_FAULT,
1087 }, {
1088 .reg = PMBUS_VIN_OV_WARN_LIMIT,
1089 .attr = "max",
1090 .alarm = "max_alarm",
1091 .sbit = PB_VOLTAGE_OV_WARNING,
1092 }, {
1093 .reg = PMBUS_VIN_OV_FAULT_LIMIT,
1094 .attr = "crit",
1095 .alarm = "crit_alarm",
1096 .sbit = PB_VOLTAGE_OV_FAULT,
1097 },
1098 };
1099
1100 static const struct pmbus_limit_attr vout_limit_attrs[] = {
1101 {
1102 .reg = PMBUS_VOUT_UV_WARN_LIMIT,
1103 .attr = "min",
1104 .alarm = "min_alarm",
1105 .sbit = PB_VOLTAGE_UV_WARNING,
1106 }, {
1107 .reg = PMBUS_VOUT_UV_FAULT_LIMIT,
1108 .attr = "lcrit",
1109 .alarm = "lcrit_alarm",
1110 .sbit = PB_VOLTAGE_UV_FAULT,
1111 }, {
1112 .reg = PMBUS_VOUT_OV_WARN_LIMIT,
1113 .attr = "max",
1114 .alarm = "max_alarm",
1115 .sbit = PB_VOLTAGE_OV_WARNING,
1116 }, {
1117 .reg = PMBUS_VOUT_OV_FAULT_LIMIT,
1118 .attr = "crit",
1119 .alarm = "crit_alarm",
1120 .sbit = PB_VOLTAGE_OV_FAULT,
1121 }
1122 };
1123
1124 static const struct pmbus_sensor_attr voltage_attributes[] = {
1125 {
1126 .reg = PMBUS_READ_VIN,
1127 .class = PSC_VOLTAGE_IN,
1128 .label = "vin",
1129 .func = PMBUS_HAVE_VIN,
1130 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1131 .sbase = PB_STATUS_INPUT_BASE,
1132 .gbit = PB_STATUS_VIN_UV,
1133 .limit = vin_limit_attrs,
1134 .nlimit = ARRAY_SIZE(vin_limit_attrs),
1135 }, {
1136 .reg = PMBUS_READ_VCAP,
1137 .class = PSC_VOLTAGE_IN,
1138 .label = "vcap",
1139 .func = PMBUS_HAVE_VCAP,
1140 }, {
1141 .reg = PMBUS_READ_VOUT,
1142 .class = PSC_VOLTAGE_OUT,
1143 .label = "vout",
1144 .paged = true,
1145 .func = PMBUS_HAVE_VOUT,
1146 .sfunc = PMBUS_HAVE_STATUS_VOUT,
1147 .sbase = PB_STATUS_VOUT_BASE,
1148 .gbit = PB_STATUS_VOUT_OV,
1149 .limit = vout_limit_attrs,
1150 .nlimit = ARRAY_SIZE(vout_limit_attrs),
1151 }
1152 };
1153
1154 /* Current attributes */
1155
1156 static const struct pmbus_limit_attr iin_limit_attrs[] = {
1157 {
1158 .reg = PMBUS_IIN_OC_WARN_LIMIT,
1159 .attr = "max",
1160 .alarm = "max_alarm",
1161 .sbit = PB_IIN_OC_WARNING,
1162 }, {
1163 .reg = PMBUS_IIN_OC_FAULT_LIMIT,
1164 .attr = "crit",
1165 .alarm = "crit_alarm",
1166 .sbit = PB_IIN_OC_FAULT,
1167 }
1168 };
1169
1170 static const struct pmbus_limit_attr iout_limit_attrs[] = {
1171 {
1172 .reg = PMBUS_IOUT_OC_WARN_LIMIT,
1173 .attr = "max",
1174 .alarm = "max_alarm",
1175 .sbit = PB_IOUT_OC_WARNING,
1176 }, {
1177 .reg = PMBUS_IOUT_UC_FAULT_LIMIT,
1178 .attr = "lcrit",
1179 .alarm = "lcrit_alarm",
1180 .sbit = PB_IOUT_UC_FAULT,
1181 }, {
1182 .reg = PMBUS_IOUT_OC_FAULT_LIMIT,
1183 .attr = "crit",
1184 .alarm = "crit_alarm",
1185 .sbit = PB_IOUT_OC_FAULT,
1186 }
1187 };
1188
1189 static const struct pmbus_sensor_attr current_attributes[] = {
1190 {
1191 .reg = PMBUS_READ_IIN,
1192 .class = PSC_CURRENT_IN,
1193 .label = "iin",
1194 .func = PMBUS_HAVE_IIN,
1195 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1196 .sbase = PB_STATUS_INPUT_BASE,
1197 .limit = iin_limit_attrs,
1198 .nlimit = ARRAY_SIZE(iin_limit_attrs),
1199 }, {
1200 .reg = PMBUS_READ_IOUT,
1201 .class = PSC_CURRENT_OUT,
1202 .label = "iout",
1203 .paged = true,
1204 .func = PMBUS_HAVE_IOUT,
1205 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1206 .sbase = PB_STATUS_IOUT_BASE,
1207 .gbit = PB_STATUS_IOUT_OC,
1208 .limit = iout_limit_attrs,
1209 .nlimit = ARRAY_SIZE(iout_limit_attrs),
1210 }
1211 };
1212
1213 /* Power attributes */
1214
1215 static const struct pmbus_limit_attr pin_limit_attrs[] = {
1216 {
1217 .reg = PMBUS_PIN_OP_WARN_LIMIT,
1218 .attr = "max",
1219 .alarm = "alarm",
1220 .sbit = PB_PIN_OP_WARNING,
1221 }
1222 };
1223
1224 static const struct pmbus_limit_attr pout_limit_attrs[] = {
1225 {
1226 .reg = PMBUS_POUT_MAX,
1227 .attr = "cap",
1228 .alarm = "cap_alarm",
1229 .sbit = PB_POWER_LIMITING,
1230 }, {
1231 .reg = PMBUS_POUT_OP_WARN_LIMIT,
1232 .attr = "max",
1233 .alarm = "max_alarm",
1234 .sbit = PB_POUT_OP_WARNING,
1235 }, {
1236 .reg = PMBUS_POUT_OP_FAULT_LIMIT,
1237 .attr = "crit",
1238 .alarm = "crit_alarm",
1239 .sbit = PB_POUT_OP_FAULT,
1240 }
1241 };
1242
1243 static const struct pmbus_sensor_attr power_attributes[] = {
1244 {
1245 .reg = PMBUS_READ_PIN,
1246 .class = PSC_POWER,
1247 .label = "pin",
1248 .func = PMBUS_HAVE_PIN,
1249 .sfunc = PMBUS_HAVE_STATUS_INPUT,
1250 .sbase = PB_STATUS_INPUT_BASE,
1251 .limit = pin_limit_attrs,
1252 .nlimit = ARRAY_SIZE(pin_limit_attrs),
1253 }, {
1254 .reg = PMBUS_READ_POUT,
1255 .class = PSC_POWER,
1256 .label = "pout",
1257 .paged = true,
1258 .func = PMBUS_HAVE_POUT,
1259 .sfunc = PMBUS_HAVE_STATUS_IOUT,
1260 .sbase = PB_STATUS_IOUT_BASE,
1261 .limit = pout_limit_attrs,
1262 .nlimit = ARRAY_SIZE(pout_limit_attrs),
1263 }
1264 };
1265
1266 /* Temperature atributes */
1267
1268 static const struct pmbus_limit_attr temp_limit_attrs[] = {
1269 {
1270 .reg = PMBUS_UT_WARN_LIMIT,
1271 .attr = "min",
1272 .alarm = "min_alarm",
1273 .sbit = PB_TEMP_UT_WARNING,
1274 }, {
1275 .reg = PMBUS_UT_FAULT_LIMIT,
1276 .attr = "lcrit",
1277 .alarm = "lcrit_alarm",
1278 .sbit = PB_TEMP_UT_FAULT,
1279 }, {
1280 .reg = PMBUS_OT_WARN_LIMIT,
1281 .attr = "max",
1282 .alarm = "max_alarm",
1283 .sbit = PB_TEMP_OT_WARNING,
1284 }, {
1285 .reg = PMBUS_OT_FAULT_LIMIT,
1286 .attr = "crit",
1287 .alarm = "crit_alarm",
1288 .sbit = PB_TEMP_OT_FAULT,
1289 }
1290 };
1291
1292 static const struct pmbus_sensor_attr temp_attributes[] = {
1293 {
1294 .reg = PMBUS_READ_TEMPERATURE_1,
1295 .class = PSC_TEMPERATURE,
1296 .paged = true,
1297 .update = true,
1298 .compare = true,
1299 .func = PMBUS_HAVE_TEMP,
1300 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1301 .sbase = PB_STATUS_TEMP_BASE,
1302 .gbit = PB_STATUS_TEMPERATURE,
1303 .limit = temp_limit_attrs,
1304 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1305 }, {
1306 .reg = PMBUS_READ_TEMPERATURE_2,
1307 .class = PSC_TEMPERATURE,
1308 .paged = true,
1309 .update = true,
1310 .compare = true,
1311 .func = PMBUS_HAVE_TEMP2,
1312 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1313 .sbase = PB_STATUS_TEMP_BASE,
1314 .gbit = PB_STATUS_TEMPERATURE,
1315 .limit = temp_limit_attrs,
1316 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1317 }, {
1318 .reg = PMBUS_READ_TEMPERATURE_3,
1319 .class = PSC_TEMPERATURE,
1320 .paged = true,
1321 .update = true,
1322 .compare = true,
1323 .func = PMBUS_HAVE_TEMP3,
1324 .sfunc = PMBUS_HAVE_STATUS_TEMP,
1325 .sbase = PB_STATUS_TEMP_BASE,
1326 .gbit = PB_STATUS_TEMPERATURE,
1327 .limit = temp_limit_attrs,
1328 .nlimit = ARRAY_SIZE(temp_limit_attrs),
1329 }
1330 };
1331
1332 static const int pmbus_fan_registers[] = {
1333 PMBUS_READ_FAN_SPEED_1,
1334 PMBUS_READ_FAN_SPEED_2,
1335 PMBUS_READ_FAN_SPEED_3,
1336 PMBUS_READ_FAN_SPEED_4
1337 };
1338
1339 static const int pmbus_fan_config_registers[] = {
1340 PMBUS_FAN_CONFIG_12,
1341 PMBUS_FAN_CONFIG_12,
1342 PMBUS_FAN_CONFIG_34,
1343 PMBUS_FAN_CONFIG_34
1344 };
1345
1346 static const int pmbus_fan_status_registers[] = {
1347 PMBUS_STATUS_FAN_12,
1348 PMBUS_STATUS_FAN_12,
1349 PMBUS_STATUS_FAN_34,
1350 PMBUS_STATUS_FAN_34
1351 };
1352
1353 static const u32 pmbus_fan_flags[] = {
1354 PMBUS_HAVE_FAN12,
1355 PMBUS_HAVE_FAN12,
1356 PMBUS_HAVE_FAN34,
1357 PMBUS_HAVE_FAN34
1358 };
1359
1360 static const u32 pmbus_fan_status_flags[] = {
1361 PMBUS_HAVE_STATUS_FAN12,
1362 PMBUS_HAVE_STATUS_FAN12,
1363 PMBUS_HAVE_STATUS_FAN34,
1364 PMBUS_HAVE_STATUS_FAN34
1365 };
1366
1367 /* Fans */
1368 static void pmbus_add_fan_attributes(struct i2c_client *client,
1369 struct pmbus_data *data)
1370 {
1371 const struct pmbus_driver_info *info = data->info;
1372 int index = 1;
1373 int page;
1374
1375 for (page = 0; page < info->pages; page++) {
1376 int f;
1377
1378 for (f = 0; f < ARRAY_SIZE(pmbus_fan_registers); f++) {
1379 int regval;
1380
1381 if (!(info->func[page] & pmbus_fan_flags[f]))
1382 break;
1383
1384 if (!pmbus_check_word_register(client, page,
1385 pmbus_fan_registers[f]))
1386 break;
1387
1388 /*
1389 * Skip fan if not installed.
1390 * Each fan configuration register covers multiple fans,
1391 * so we have to do some magic.
1392 */
1393 regval = _pmbus_read_byte_data(client, page,
1394 pmbus_fan_config_registers[f]);
1395 if (regval < 0 ||
1396 (!(regval & (PB_FAN_1_INSTALLED >> ((f & 1) * 4)))))
1397 continue;
1398
1399 pmbus_add_sensor(data, "fan", "input", index, page,
1400 pmbus_fan_registers[f], PSC_FAN, true,
1401 true);
1402
1403 /*
1404 * Each fan status register covers multiple fans,
1405 * so we have to do some magic.
1406 */
1407 if ((info->func[page] & pmbus_fan_status_flags[f]) &&
1408 pmbus_check_byte_register(client,
1409 page, pmbus_fan_status_registers[f])) {
1410 int base;
1411
1412 if (f > 1) /* fan 3, 4 */
1413 base = PB_STATUS_FAN34_BASE + page;
1414 else
1415 base = PB_STATUS_FAN_BASE + page;
1416 pmbus_add_boolean_reg(data, "fan", "alarm",
1417 index, base,
1418 PB_FAN_FAN1_WARNING >> (f & 1));
1419 pmbus_add_boolean_reg(data, "fan", "fault",
1420 index, base,
1421 PB_FAN_FAN1_FAULT >> (f & 1));
1422 }
1423 index++;
1424 }
1425 }
1426 }
1427
1428 static void pmbus_find_attributes(struct i2c_client *client,
1429 struct pmbus_data *data)
1430 {
1431 /* Voltage sensors */
1432 pmbus_add_sensor_attrs(client, data, "in", voltage_attributes,
1433 ARRAY_SIZE(voltage_attributes));
1434
1435 /* Current sensors */
1436 pmbus_add_sensor_attrs(client, data, "curr", current_attributes,
1437 ARRAY_SIZE(current_attributes));
1438
1439 /* Power sensors */
1440 pmbus_add_sensor_attrs(client, data, "power", power_attributes,
1441 ARRAY_SIZE(power_attributes));
1442
1443 /* Temperature sensors */
1444 pmbus_add_sensor_attrs(client, data, "temp", temp_attributes,
1445 ARRAY_SIZE(temp_attributes));
1446
1447 /* Fans */
1448 pmbus_add_fan_attributes(client, data);
1449 }
1450
1451 /*
1452 * Identify chip parameters.
1453 * This function is called for all chips.
1454 */
1455 static int pmbus_identify_common(struct i2c_client *client,
1456 struct pmbus_data *data)
1457 {
1458 int vout_mode = -1, exponent;
1459
1460 if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE))
1461 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
1462 if (vout_mode >= 0 && vout_mode != 0xff) {
1463 /*
1464 * Not all chips support the VOUT_MODE command,
1465 * so a failure to read it is not an error.
1466 */
1467 switch (vout_mode >> 5) {
1468 case 0: /* linear mode */
1469 if (data->info->format[PSC_VOLTAGE_OUT] != linear)
1470 return -ENODEV;
1471
1472 exponent = vout_mode & 0x1f;
1473 /* and sign-extend it */
1474 if (exponent & 0x10)
1475 exponent |= ~0x1f;
1476 data->exponent = exponent;
1477 break;
1478 case 1: /* VID mode */
1479 if (data->info->format[PSC_VOLTAGE_OUT] != vid)
1480 return -ENODEV;
1481 break;
1482 case 2: /* direct mode */
1483 if (data->info->format[PSC_VOLTAGE_OUT] != direct)
1484 return -ENODEV;
1485 break;
1486 default:
1487 return -ENODEV;
1488 }
1489 }
1490
1491 /* Determine maximum number of sensors, booleans, and labels */
1492 pmbus_find_max_attr(client, data);
1493 pmbus_clear_fault_page(client, 0);
1494 return 0;
1495 }
1496
1497 int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
1498 struct pmbus_driver_info *info)
1499 {
1500 const struct pmbus_platform_data *pdata = client->dev.platform_data;
1501 struct pmbus_data *data;
1502 int ret;
1503
1504 if (!info) {
1505 dev_err(&client->dev, "Missing chip information");
1506 return -ENODEV;
1507 }
1508
1509 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WRITE_BYTE
1510 | I2C_FUNC_SMBUS_BYTE_DATA
1511 | I2C_FUNC_SMBUS_WORD_DATA))
1512 return -ENODEV;
1513
1514 data = kzalloc(sizeof(*data), GFP_KERNEL);
1515 if (!data) {
1516 dev_err(&client->dev, "No memory to allocate driver data\n");
1517 return -ENOMEM;
1518 }
1519
1520 i2c_set_clientdata(client, data);
1521 mutex_init(&data->update_lock);
1522
1523 /* Bail out if PMBus status register does not exist. */
1524 if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0) {
1525 dev_err(&client->dev, "PMBus status register not found\n");
1526 ret = -ENODEV;
1527 goto out_data;
1528 }
1529
1530 if (pdata)
1531 data->flags = pdata->flags;
1532 data->info = info;
1533
1534 pmbus_clear_faults(client);
1535
1536 if (info->identify) {
1537 ret = (*info->identify)(client, info);
1538 if (ret < 0) {
1539 dev_err(&client->dev, "Chip identification failed\n");
1540 goto out_data;
1541 }
1542 }
1543
1544 if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
1545 dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
1546 info->pages);
1547 ret = -EINVAL;
1548 goto out_data;
1549 }
1550
1551 ret = pmbus_identify_common(client, data);
1552 if (ret < 0) {
1553 dev_err(&client->dev, "Failed to identify chip capabilities\n");
1554 goto out_data;
1555 }
1556
1557 ret = -ENOMEM;
1558 data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors,
1559 GFP_KERNEL);
1560 if (!data->sensors) {
1561 dev_err(&client->dev, "No memory to allocate sensor data\n");
1562 goto out_data;
1563 }
1564
1565 data->booleans = kzalloc(sizeof(struct pmbus_boolean)
1566 * data->max_booleans, GFP_KERNEL);
1567 if (!data->booleans) {
1568 dev_err(&client->dev, "No memory to allocate boolean data\n");
1569 goto out_sensors;
1570 }
1571
1572 data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels,
1573 GFP_KERNEL);
1574 if (!data->labels) {
1575 dev_err(&client->dev, "No memory to allocate label data\n");
1576 goto out_booleans;
1577 }
1578
1579 data->attributes = kzalloc(sizeof(struct attribute *)
1580 * data->max_attributes, GFP_KERNEL);
1581 if (!data->attributes) {
1582 dev_err(&client->dev, "No memory to allocate attribute data\n");
1583 goto out_labels;
1584 }
1585
1586 pmbus_find_attributes(client, data);
1587
1588 /*
1589 * If there are no attributes, something is wrong.
1590 * Bail out instead of trying to register nothing.
1591 */
1592 if (!data->num_attributes) {
1593 dev_err(&client->dev, "No attributes found\n");
1594 ret = -ENODEV;
1595 goto out_attributes;
1596 }
1597
1598 /* Register sysfs hooks */
1599 data->group.attrs = data->attributes;
1600 ret = sysfs_create_group(&client->dev.kobj, &data->group);
1601 if (ret) {
1602 dev_err(&client->dev, "Failed to create sysfs entries\n");
1603 goto out_attributes;
1604 }
1605 data->hwmon_dev = hwmon_device_register(&client->dev);
1606 if (IS_ERR(data->hwmon_dev)) {
1607 ret = PTR_ERR(data->hwmon_dev);
1608 dev_err(&client->dev, "Failed to register hwmon device\n");
1609 goto out_hwmon_device_register;
1610 }
1611 return 0;
1612
1613 out_hwmon_device_register:
1614 sysfs_remove_group(&client->dev.kobj, &data->group);
1615 out_attributes:
1616 kfree(data->attributes);
1617 out_labels:
1618 kfree(data->labels);
1619 out_booleans:
1620 kfree(data->booleans);
1621 out_sensors:
1622 kfree(data->sensors);
1623 out_data:
1624 kfree(data);
1625 return ret;
1626 }
1627 EXPORT_SYMBOL_GPL(pmbus_do_probe);
1628
1629 int pmbus_do_remove(struct i2c_client *client)
1630 {
1631 struct pmbus_data *data = i2c_get_clientdata(client);
1632 hwmon_device_unregister(data->hwmon_dev);
1633 sysfs_remove_group(&client->dev.kobj, &data->group);
1634 kfree(data->attributes);
1635 kfree(data->labels);
1636 kfree(data->booleans);
1637 kfree(data->sensors);
1638 kfree(data);
1639 return 0;
1640 }
1641 EXPORT_SYMBOL_GPL(pmbus_do_remove);
1642
1643 MODULE_AUTHOR("Guenter Roeck");
1644 MODULE_DESCRIPTION("PMBus core driver");
1645 MODULE_LICENSE("GPL");
This page took 0.063198 seconds and 6 git commands to generate.