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